1,105,380 Community Members

Remove filename from url

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
0
 

Try:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME}\.php
RewriteRule ^(.*)$ $1.php
Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
0
 

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

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
0
 

ok, how about:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.(php|html?)
RewriteRule ^(.*)$ $1.(php|html?)
Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Nope. Still 404 error

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
1
 

Have you tried just the php snippet:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Still comes up with a 404

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
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!

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
0
 

Also page refresh the browser using ctrl + f5

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Squidge
Posting Pro in Training
476 posts since Dec 2009
Reputation Points: 101 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 6 [?]
 
0
 

I would suggest in that case your install is incorrect

Member Avatar
fheppell
Junior Poster
123 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: