I've got this htacces code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

I can load html files without the extension (test.html becomes test) but a php file in the same directory gives a 404 error when I try and do the same.
Why is is it doing this?

Recommended Answers

All 16 Replies


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME}\.php
RewriteRule ^(.*)$ $1.php

Still comes up with a 404 error. Changed it for all htaccess files

have you removed the '.php' from page links?

Yes. Let's say I want to visit localhost/test.php, I go to localhost/test but it gives me a 404 error

ok, how about:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.(php|html?)
RewriteRule ^(.*)$ $1.(php|html?)

Nope. Still 404 error

Have you tried just the php snippet:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Still comes up with a 404

I think it might be a php problem because a wordpress installation isn't working either

When I delete every .htaccess file on the server the .html files still work without the extension. Why is this?

It's probably cached by the browser, try by adding a random value to the requested url:


During the test, on each request, change the numbers (i.e. this ?1234) so you will be sure to get new content. Also the rules suggested by Squidge are working fine for me. Bye!

Also page refresh the browser using ctrl + f5

Tried both of those, still nothing. I installed a webserver in exactly the same way on another linux machine and I have the same problem, html files work, php ones don't

I would suggest in that case your install is incorrect

But how would two installs on separate machines be incorrect. I also ran the command to enable mod_rewrite on both webservers, but still not working.

Check the config file of the website, maybe the problem is given by these directives:

<Directory /var/www/website/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

Now, if MultiViews is enabled (i.e. without the minus) if you digit /page the server will attemp to find a file page.html or a directory /page/ inside the server root and this is probably the reason why your rewrite rule seems to work properly with html files.

Secondly, usually in fresh installs AllowOverride is set to None, in these cases the server will ignore the .htaccess file, so try to change it as the above, to do that disable the website:

sudo a2dissite website

browse to /etc/apache2/sites-available/ and modify the file, then reanable and reload the server:

sudo a2ensite webiste
sudo service apache2 reload

if reload doesn't work use restart and it should work fine. Here you can find more info: http://httpd.apache.org/docs/current/mod/core.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.