2

I just installed mod_rewrite and enabled .htaccess on my ubuntu apache server. But the url wont change when I edit the .htaccess to remove the .php file extension from files.

Right now my .htaccess file look like this:

Options -Indexes

ErrorDocument 400 /blabla.php
ErrorDocument 401 /blabla.php
ErrorDocument 403 /blabla.php
ErrorDocument 404 /blabla.php
ErrorDocument 500 /blabla.php

Not much, just making sure ppl cant view my filetree and added some custom error pages.

But if I enter a page on my site eg href="page.php" everything works, but if I remove the ".php" from the url bar and reload the page as "www.mysite.com/page" without the .php it still works. And I havent done anything in the .htaccess file, is this right?

if I now add the following lines to the .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

And then visit href="page.php" the url still shows up as "www.mysite.com/page.php" instead of "www.mysite.com/page"

Do I also need to change the href from href="page.php" to href="page" ? In order to make the .php extension disappear. - Because that works even if I dont edit the .htaccess file in the first place.

And I always make sure to restart apache when I edit the .htaccess file.

Thanks in advance, K

user2722667
  • 6,435
  • 14
  • 41
  • 85

3 Answers3

4

You have asked many questions here. Let me answer first one:

www.mysite.com/page works without any rewrite rule due to Options MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

If you place this line at top of your root .htaccess:

Options -MultiViews

Then www.mysite.com/page will issue 404.

Also to automatically remove .php from your URLs use this code in your root .htaccess:

Options -MultiViews
RewriteEngine On

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • It works but links gets borken, my url turs up as "www.mysite.com:8080/page" No idea why the ":8080" shows up, could be something caused since I installed varnish? – user2722667 Jul 29 '14 at 13:54
  • Yes at least none of these rules are adding `:8080` anywhere. – anubhava Jul 29 '14 at 13:55
  • Ok, so if my href looks like href="page.php" i will get redirected to "www.mysite.com:8080/page" and that wont work.. But if my href is href="page" I get redirected to "www.mysite.com/page" and it works. Its just soon as I add .php to the url and reload the page it redirects me to a :8080 – user2722667 Jul 29 '14 at 15:34
  • Try to replace your links like this: `href="/page.php"` – anubhava Jul 29 '14 at 15:51
  • Thanks for your answer, but it didnt help me. Same thing again soon as .php is in the link it gets broken. I think it has to do with apache/varnish. Prob Apache listening on the public interface instead on the loopback interface. – user2722667 Jul 29 '14 at 16:38
  • Hmm ok, Is there any reference to `8080` in your Apache config? – anubhava Jul 29 '14 at 16:39
  • Yeah, What I followed this tutorial on digitalocean. https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-varnish-with-apache-on-ubuntu-12-04--3 – user2722667 Jul 30 '14 at 10:20
  • Open Firebug and then visit `http://domain.com/page.php` and see what redirects you get in `Net` tab. – anubhava Jul 30 '14 at 10:35
0
  1. If page automatically renders page.php you have enabled Content Negotiation, most probably specifically MultiViews on the httpd.conf level.
  2. Yes, you have to link to what you want to appear in the address bar.
Niels Keurentjes
  • 38,099
  • 7
  • 85
  • 126
0

Try this.

RewriteRule (.*)\ $1.php
RajivRisi
  • 398
  • 1
  • 11