0

I'm having trouble removing the .php file extensions from URLs using the .htaccess file.

Previously, all pages were HTML and I had successfully rewritten URLs to drop the .html extensions, but it just doesn't seem to be working for .php — I'm just getting 404 errors.

.htaccess Contents

ErrorDocument 404 http://www.brianglassmandesign.com/404.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^brianglassmandesign.com [NC]
RewriteRule ^(.*)$ http://www.brianglassmandesign.com/$1 [L,R=301,NC]

# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
3
  • 1
    The last line is matching on .* which includes the extension. What happens when you replace that with something that matches up to, but not including, the extension? Like RewriteRule ^([^\.]+)$ $1.php [NC,L] Commented Apr 25, 2016 at 16:04
  • @Anton, if the OP requests /something, surely it should resolve to /something.php anyway? The only change (seemingly) the OP made was from .html to .php... Commented Apr 25, 2016 at 17:08
  • You're right @MikeRockett, it shouldn't matter. Commented Apr 25, 2016 at 17:11

1 Answer 1

1

There is no reason as to why the files are not being picked up, giving consideration to the fact that the only change you made was the extension. As such, I recommend that you double-check to see if the file being requested actually exists, and has been renamed from the old .html equivalent.

The only .htaccess suggestion I can offer that is an alternative to what you have is the following:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) $1.php [L]

This means that the rewrite will only occur if the file exists. So, if you request /something and /something.php exists on the file-system, then it will be served accordingly.

Failing which, you'll need to check the relevant logs to determine what the problem may be.

Sign up to request clarification or add additional context in comments.

2 Comments

You've solved the riddle, my friend.After creating /something and updating .htaccess with your modifications — everything seems to be working properly. Thank you!
Great! Please accept this answer for the benefit of others.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.