2

I originally wanted to have all my urls end with no extension. Unfortunately, I've tried many htaccess codes and I've just about given up.

So now I want to make it so if a person wants to visit a page in my site, but forgets to enter .php, he/she will automatically be redirected to the same url but with the .php

How can this be done? Thanks!

user758287
  • 137
  • 1
  • 2
  • 8

4 Answers4

5

Here are the rules:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
  1. It will check if requested resource is not a existing folder. For example: you requesting http://www.example.com/help. If there is such folder present (/help) the rule will do nothing (priority is given to a folder). If you do not want this behaviour then remove the first line.

  2. It will check if there is such .php file before rewriting. For example: you requesting http://www.example.com/aboutus but there is NO aboutus.php file there -- no rewrite will occur.

  3. All such requests should be without trailing slash: should be http://www.example.com/aboutus and NOT http://www.example.com/aboutus/

  4. The rule will work for URL in subfolders as well: e.g. http://www.example.com/pages/help/aboutus will be rewritten just fine.

  5. Because of the above checks the rule will not enter into a rewrite loop (no 500 error on this rule)

  6. Query string (page parameters) will be preserved

LazyOne
  • 137,403
  • 37
  • 338
  • 342
4
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Sun Love
  • 660
  • 5
  • 9
1

In response to Sun Love, the code you posted works well except for situations where you have a trailing slash without the file extension (I get a 500 error) because the first RegEx doesn't match for this situation.

example.com/test.html -- works (redirects to /test)

example.com/test -- works (no redirect)

example.com/test.html -- works (redirects to /test)

example.com/test/ -- doesn't work (500 error)

There is probably a better way to do this but I added another rewrite condition to fix this:

RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php
RewriteRule ^ %1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Erik Berkun-Drevnig
  • 2,129
  • 19
  • 36
0

Try this .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^([^\.]+)$ $1.php [L]

EDIT: ^([^\.]+)$ $1.php [L]

Anthony Simmon
  • 1,530
  • 12
  • 23
  • I already started my htaccess code with RewriteEngine on RewriteBase / will it affect my other htaccess codes if I add the Options+FollowSymLinks at the beginning? If yes, what should i do? – user758287 Jul 07 '11 at 03:04
  • I don't think that it will affect your current .htaccess. It specifies whether or not apache should follow a symbolic link while looking for a file. For example, if you have a file `/home/mydomain/www/myfile.html` which is a link to `/home/mydomain/somedir/therealfile.html` Then apache will only retrieve the realfile.html if you have that option enabled. Just try. More info at [http://www.maxi-pedia.com/FollowSymLinks](http://www.maxi-pedia.com/FollowSymLinks) – Anthony Simmon Jul 07 '11 at 03:17
  • Well I just tried RewriteRule ^(.*)$ $1.php [L] without the Options and I got a 500 error. I added the Options +FollowSymLinks and I still got a 500 error. Hmm, :-/ – user758287 Jul 07 '11 at 03:33
  • Try `RewriteRule ^([^\.]+)$ $1.php [L]`. I am almost 99% sure that it will work. – Anthony Simmon Jul 07 '11 at 04:00