1.11M Members

Remove filename from url

 
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?

 
0
 

Try:

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

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

 
0
 

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

 
0
 

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

 
0
 

ok, how about:

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

Nope. Still 404 error

 
1
 

Have you tried just the php snippet:

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

Still comes up with a 404

 
0
 

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

 
0
 

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

 
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!

 
0
 

Also page refresh the browser using ctrl + f5

 
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

 
0
 

I would suggest in that case your install is incorrect

 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: