Remove filename from url
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?
Related Article: URL Parsing and Validation
is a PHP discussion thread by densman that has 3 replies, was last updated 10 months ago and has been tagged with the keywords: url, php, server.
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Still comes up with a 404 error. Changed it for all htaccess files
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Yes. Let's say I want to visit localhost/test.php, I go to localhost/test but it gives me a 404 error
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Still comes up with a 404
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
I think it might be a php problem because a wordpress installation isn't working either
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
When I delete every .htaccess file on the server the .html files still work without the extension. Why is this?
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
It's probably cached by the browser, try by adding a random value to the requested url:
http://localhost/page?1234
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!
cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22
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
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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.
fheppell
Junior Poster in Training
53 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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
</Directory>
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
cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22