I am having an issue with using an .htaccess file to rewrite my urls without .php in them. It works for some addresses, but not others. Here is what is in my .htaccess file..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
#rewrite url up to 3 levels
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /$1/$2/$3.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I have the following folder/file structure on my site..
index.php
about-us
index.php
careers.php
our-work
index.php
test.php
client1
example.php
The following urls work..
example.com
example.com/about-us
example.com/our-work
example.com/our-work/client1/example
But these do not work..
example.com/our-work/test
example.com/about-us/careers
I just get a 404 error.
Any idea why this is happening?
You don’t have a 2 level rule, just a 1 level and 3 level rule. You should add:
You should probably prefix your 3 level rule with the RewriteCond conditions also:
This will prevent the rule being invoked if there is a directory with the same name or the PHP file does not exist.