Skip to content
Advertisement

Relative URL not loading PHP file, but exact URL does

Relevant code:

if(strpos($_SERVER['REQUEST_URI'],'/admin/') === 0 ){
    include_once 'admin.php';
    exit;
}

if(strpos($_SERVER['REQUEST_URI'],'pc-repair-') !== FALSE && strpos($_SERVER['REQUEST_URI'],'-computer-services') !== FALSE ){
    include_once 'locations/default.php';
    exit;
}

include_once 'inl/head.php';

Example link on site: <a href="/about/special-offers/">Special offers</a>

Trying to make the request to /pc-repair-dallas-tx-computer-services/ but the PHP file doesn’t actually load…

PHP is working and short codes are enabled. I’m not a PHP developer so I’m out of my element here but would appreciate any advice to get these relative URLs working. They are broken site-wide. (eg. /about/special-offers/ does not load but /about/special-offers.php does)

So it seems the direction I need to take is a rewrite rule in my .htaccess file. Now, doing this for each file seems gratuitous, so I’m trying to write a rule that will automatically direct from /page to /page.php. Here’s what I’ve got so far in my .htaccess

php_value short_open_tag 1
Options +MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]+)$ $1.php [NC,L]
</IfModule>

EDIT:

I’m now able to access my files using the desired URL scheme. The last problem lies within the code at the top, it is not including admin.php when the requested URL is at server.com/admin/

Advertisement

Answer

You cannon access the /about/special-offers/ URL and expect your server to return /about/special-offers.php, at least unless specifying correct rewrite rules in your .htaccess file. Just use the URL with extension /about/special-offers.php.

If you want to avoid using PHP file extensions in your URLs then look for rewriting engine in Apache.

In your specific case, I would make sure the mod_rewrite module is enabled on your server configuration and added the following .htaccess to the root of your project:

RewriteEngine On
RewriteRule ^about/special-offers/?$ about/special-offers.php

Here’s the article that might be interesting to you: mod_rewrite: A Beginner’s Guide to URL Rewriting

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement