.htaccess : Hiding .php extensions in URL

Hi

I was wondering if there is anyway we can hide the .php extension in the URL using .htaccess?

For example

These URLs:

http://example.com/home.php
http://example.com/welcome/contact-us.php
http://example.com/home/support/email_us.php

Should look like

http://example.com/home
http://example.com/welcome/contact-us
http://example.com/home/support/email_us

Pls let me know if this is possible and how?

Many thanks

Try this out:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\\.php [L]

You still have to change all the links on your site to point to the new URLs.

c10,

Obviously, YOU have to create the links to those pages without the .php extension.

Dan,

NICELY DONE!

RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\\.php [L]
# redirect to the php script with the requested filename

Regards,

DK

Hi Guys

Thanks for your replies.

Can we add something to this so that it accepts parameters like http://domain.com/user/name/john

which is similar to http://domain.com/user.php?name=john

Thanks in advance

c10,

If you want something for a generic URI like that (lowercase characters only), try:

RewriteEngine on
RewriteRule ^user/([a-z]+/([a-z]+)$ user.php?$1=$2 [L]

It’s all very simple when you can specify exactly what you want and understand enough regex to translate it to code. If you can’t, try the Article in my signature.

Regards,

DK

Hi

I do not want to hard-code the user.php file in the .htaccess since the page name can be anything, so is it possible to to have /var/value type parameters without hard-coding the file name?

Thanks

What about when you want more than one /var/value on the end of the URL? At some point your rules are going to be too complex to want to manage them as rewrite rules, and you should just edit your .php files to look at the URL and parse them there.

Most sites with completely arbitrary URLs that can map to many different actions just rewrite all requests to a single file, and that file’s job is to parse the URL and invoke the correct code.

Front Controller pattern - Wikipedia, the free encyclopedia

Dan,

Too true! However, that’s a “cheat” which involves much more sophistication by the member who was only asking a simple mod_rewrite question. If he still wanted to use other scripts, “user” above could have been made a variable, too, in the regex and redirection (and remained a very simple question).

Regards,

DK

In continuation to my question, can I use something like this:

RewriteEngine on
RewriteRule ^*/([a-z]+/([a-z]+)$ *.php?$1=$2 [L]

c10,

No. I’ve never seen either regex or a redirection like that as it makes no sense at all. Try

RewriteEngine on
RewriteRule ([a-z]+)/([a-z]+)/([a-z]+)$ $1.php?$2=$3 [L]

which would capture the filename of the php script (without extenstion) as well as the key and value for a query string and redirect properly.

It would REALLY help if you would provide a verbal description of what YOU think your code does.

Regards,

DK