How to hide the extension of a page from URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

How to hide the extension of a page from URL

Post by dheeraja »

Hi Guys,
I want to hide the page extension from URL but not getting how to do this stuff, suppose i m having a url "www.example.com/contact.php" then I just want that browser should show "www.example.com/contact".

Thank you
Dheeraj
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to hide the extension of a page from URL

Post by requinix »

If you have Apache you can use the MultiViews option and it'll add the extension behind the scenes for you; URLs both with and without extension will work. If you want to forcibly remove the extension from the URL then you'll need URL rewriting as well.

Code: Select all

# in your .htaccess file
Options +MultiViews
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

Re: How to hide the extension of a page from URL

Post by dheeraja »

Ya I want to forcibly remove the extension from URL, but not getting that what will be the Code...
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How to hide the extension of a page from URL

Post by klevis miho »

You should do a URL rewrite in apache, and change the way the link is called.
For example in you code now it is: <a href="/contact.php">Contact</a>
You should change it to: <a href="/contact">Contact</a>

And it the htaccess put this line:

RewriteEngine on
RewriteRule ^contact$ /contact.php [R=301,L]
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

Re: How to hide the extension of a page from URL

Post by dheeraja »

so should I have to write all pages name like
RewriteRule ^contact$ /contact.php [R=301,L]
RewriteRule ^abc$ /abc.php [R=301,L]
RewriteRule ^xyz$ /xyz.php [R=301,L]

and like wise.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to hide the extension of a page from URL

Post by requinix »

Or you could do what I mentioned and use MultiViews instead.

To forcibly remove the extension,
1. Fix the various links and stuff that you have pointing to whatever.php.
2.

Code: Select all

RewriteEngine on
RewriteRule (.*)\.php$ $1 [R]
Post Reply