2

I've been playing about with my .htaccess file and so far, it's working, but there's a few bugs.

I'm trying to force the WWW prefix (just the root, not on subdomains) while removing the .php extension and adding a trailing slash.

Code

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteRule (.*)/$ $1.php [L]


# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

It successfully force the WWW, even if I remove it, the .php extension is removed, even if I add it, and the the trailing slash is forced, even if I remove it. However, sometimes I get a 404 not found error saying that the requested URL (generally ending with the .php extension) was not found on the server, and often directories do not actually work. Edit

often directories do not actually work

meaning the server throws a 404 error saying "/directory.php was not found on this server".

Can anyone lend me a hand?

1 Answer 1

7

You need to check that you're actually rewriting to a php file before you blindly attach the php extension. So this rule:

RewriteRule (.*)/$ $1.php [L]

Needs some conditions:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

You can also avoid redirecting to include www in the hostname for subdomains if you check that there is at least 1 host before the TLD, try adding another condition to your prefix rule:

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

EDIT:

Here's what the full file should look like:

RewriteBase /

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]


# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
6
  • I thought this was it, but there's a couple of things missing! There is no forced trailing slash (I want it added if somebody removes it) and it does not remove.php if someone adds it, which I would like it to do. Jan 2, 2014 at 21:02
  • @JakeGriffin you're still using your original rules right? there just needs to be a few changes made to your original htaccess file
    – Jon Lin
    Jan 2, 2014 at 21:09
  • Sorry, I'm a little confused. I don't mean to sound stupid, but I want to make sure I'm doing it correctly. I replaced my Force WWW prefix with what you gave me and replaced my rewrite rule line with the conditions you gave too. Is this what I'm supposed to do? The www does not force and removing the trailing slash results in an error. Jan 2, 2014 at 22:59
  • See, that's exactly why my file looked like. It does not force www, there is a not found error when the trailing slash is removed. However, when the trailing slash and the www prefix is removed but .php is added, it then works perfectly, i.e adds www and trailing slash and removes the .php. Jan 2, 2014 at 23:20
  • @JakeGriffin what's your hostname? and do you have a RewriteBase?
    – Jon Lin
    Jan 2, 2014 at 23:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.