4

htaccess to remove the .php extension of my site's files.

RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]

Now, if I go to my site

www.mysite.com/home

works fine, it redirects to home.php but the URL is still friendly.

But if I write this URL:

www.mysite.com/home.php

The home.php is served, and the URL is not friendly.

How can I avoid this behavior? I want that if the user writes www.mysite.com/home.php, the URL displayed in the URL bar be www.mysite.com/home

miquel
  • 379
  • 7
  • 17

4 Answers4

10

Code

RewriteEngine On
RewriteBase /

# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]


# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Florent
  • 12,310
  • 10
  • 49
  • 58
Bhavana
  • 101
  • 1
  • 2
5

you can remove .php from requests like so

RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
scibuff
  • 13,377
  • 2
  • 27
  • 30
  • 1
    That part of the problem works. My main problem is that if the user introduces mysite.com/file.php, I want the URL to be rewritten to mysite.com/file – miquel Mar 22 '12 at 12:56
  • yes, the above will redirect `mysite.com/file.php` to `mysite.com/file` - then be aware of infinite loops - http://stackoverflow.com/questions/7823801/remove-php-extention-stop-access-of-url-with-php-extention-and-remove-trailing – scibuff Mar 22 '12 at 13:05
  • Thanks @scibuff. But is it possible to change the browser's URL? I still cannot get it modified. – miquel Mar 22 '12 at 13:25
  • use `[L,R=301]` instead of `[L,QSA]` – scibuff Mar 22 '12 at 14:54
  • Argh! Now I'm getting the `310 error: ERR_TOO_MANY_REDIRECTS` I've commented this line `#RewriteCond %{SCRIPT_FILENAME} !-f` and changed the `QSA`to `R=301` Is this so difficult to get it work? I think that I'll have to use the solution provided by @Artjom Kurapov – miquel Mar 23 '12 at 17:26
  • yes, you're almost there, read the link i posted above on how to avoid infinite loops – scibuff Mar 23 '12 at 17:48
  • Thanks @scibuff. But I don't want my "friendly url" to have the "extra" no-redirect-loop URL parameter,as I want friendly URL for SEO. – miquel Mar 23 '12 at 19:28
5

You can also put this code inside of your .htaccess file:

options +multiviews

What this does is to search for available extensions (.html, .htm and .php I think) inside the current directory. So if for example you request /user it will look for user.php.

Sorin Buturugeanu
  • 1,782
  • 4
  • 19
  • 32
  • Your answer is definitely the best one for what he wants :) – Olivier Pons Mar 23 '12 at 09:17
  • Humm, maybe not. If I put /user, then all it's ok, it redirects to /user.php. But if I put /user.php, I want the browser's URL be changed to /user also, and that's what I'm trying to do. – miquel Mar 23 '12 at 17:22
  • You could add a `htaccess` rule to redirect all `*.php` to `*` like so: `rewriterule ^(.*)\.php$ $1 [r=301,l]`. (I cannot test it right at the moment, but it should work). – Sorin Buturugeanu Mar 23 '12 at 21:31
0

Your rewrite is ignored because it checks for existing file, based on your rule:

RewriteCond %{SCRIPT_FILENAME} !-f

I would suggest adding a URL check in php to redirect to itself (without extension). Or do you want to display 404 error if someone access existing file?

Artjom Kurapov
  • 6,115
  • 4
  • 32
  • 42
  • If I remove that RewriteCond, what will be the effect? I don't see any difference. Is there a proper way to do it with .htaccess, instead of redirecting in php? – miquel Mar 22 '12 at 12:54
  • it will be routing all traffic to your file.. but since I doubt that you'll have something like home.php.php , it will result in 404. Well apache cant say to the browser to change URL in navigation to something cleaner. Mod rewrite just transforms one URL requests to others.. SO you have to state what logic you want - redirect or still serve the page.. or 404 – Artjom Kurapov Mar 22 '12 at 12:56