Hide .php extension with url rewriting using .htaccess
Posted on February 13, 2008
Filed Under htaccess, php, tips and technique, tutorial
Last time I’ve written an article about hiding php file extension where I’ve showed you how you can use .html or .asp extension of file instead of .php extension. But there was one flaw in that technique you have had to change the file extension explicitly but in this post I’m going to show you how to rewrite the URL instead of renaming the file extension Using this technique you will see product.html in the address bar of the browser but the actual file name remains product.php and you don’t need to rename the file extension. Furthermore you can rewrite the URL like product.php?id=5 to product-5.html.
what is the benefits of rewriting URL?
When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We’ll rewrite the url in such a way that in browser’s address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.
what is required for URL rewriting ??
To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know much about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?
Examples of url rewriting for seo friendly URL
For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]
The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.
Popularity: 100% [?]
If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» 5 useful url rewriting examples using .htaccess
» How to check and enable mod_rewrite module in apache
» Custom Error Page with .htaccess
» How to redirect browser to https (ssl) in php
Comments
53 Responses to “Hide .php extension with url rewriting using .htaccess”
Leave a Reply





[...] Hide .php extension with url rewriting using .htaccess By Roshan Last time I’ve written an article about hiding php file extension where I’ve showed you how you can use .html or .asp extension of file instead of .php extension. But there was one flaw in that technique you have had to change the file … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]
The “Options +FollowSymlinks” is completely unnecessary. It allows symlinks to point outside your documentroot and has nothing at all to do with rewrites.
If you have problems with .htaccess files not working, try checking your httpd.conf to make sure that it has “AllowOveride All” in it somewhere. Without this line, .htaccess files will not work at all.
great post. i liked it…
mod_rewrite turns your webserver into a super-server and frees it from the constraints of filenames.
Another trick I heard of (want to try sometime) is to turn an error page into a search. For example, if someone tries to type in a wrong page say http://yourserver.com/pagethatdoesntexist.html you redirect it w/ mod_rewrite to http://yourserver.com/search.php?query=pagethatdoesntexist so that you come up w/ links to possibly what the user was looking for instead of a ‘page not found’.
What would have to be put in .htaccess in order to rewrite both .php and .shtml extensions to .html
for rewriting .shtml you have to define another rewrite rule and add a another line like this
RewriteRule ^(.*)\.html$ $1.shtml [nc]
very good idea Michael Hazzard, I would also like to see that happening, and I would do it myself if I knew how to!
could someone email me at alex@kdams.es if they know how to do it please?
thnx
[...] Fuente | Roshan [...]
[..]Muchas veces al generar consultas en php en la barra de direcciones quedan lineas completamente inteligibles (por así llamarlo de alguna forma) repletas de signos de interrogación (?) y de igual (=)[...]
[...] my last post of Hiding .php file extension with .htaccess Michael has mentioned that you can create your own custom page for the the 404(not found) error in [...]
Short and to the point. Great post!
[...] If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess. If you don’t know something about url rewriting then please check my older post about url rewriting using .htaccess. [...]
If you rewrite .html URLs to .php internal server filepaths you MUST also block direct access to all of the .php URLs. If you don’t block them you will soon have a massive Duplicate Content issue to sort out - with all pages of the site listed with both extensions.
ya i absolutely agree with you…form seo point of view you must do that…
As for the custom 404 pages, what you want is:
ErrorDocument 404 /404.php
In either your .htaccess or your httpd.conf.
Then just create 404.php in your DocumentRoot with your custom searching code contained within. The advantages of this over a redirect to a 404.php page are:
1. A proper 404 HTTP code is sent back so that bots and caching agents will behave correctly
2. The URL doesn’t change so the user can see exactly what they typed in. They will often only want to change the one letter they mis-typed.
The relevant manual page is: http://httpd.apache.org/docs/2.0/mod/core.html#errordocument
Thanks for the link. And you can also check the following post of mine
http://roshanbh.com.np/2008/02/custom-error-page-htaccess.html
for detail about custom 404 pages.
In reply to g1smd, how would you go about blocking off access to .php files if I use this conversion from .php to .html so I don’t have to deal with the issue of duplicate content? Anyone with the answer please email me timmy0621@yahoo.com much appreciated.
Hi Friends,
I want to create SEO friendly URLs in PHP. I know that we can do from .htaccess file but other than that do we have to make some different php script also?
Following is my files:
index.php
Untitled Document
PHP is great
Opensocial in boom
newsdetails.php
<?php
echo ” in news”;
?>
.htaccess file
# //seo_mod_start
Options +Indexes +FollowSymlinks
RewriteEngine On
# Uncomment the following and add your forum path if rewrites arent working properly
RewriteRule ^index.html$ index.php [L,NE]
RewriteRule ^(.*)-a-([0-9]+).html$ newsdetails.php?nid=$1
# //seo_mod_end
But when i view index.php in browser its not showing seo friendly urls of news it shud take title of news to display.
So the final output of url shud be php-is-great.html & opensocial-in-boom.html
Can anybody please help me.
Friends in my above comment there is html code but i think its showing output of code. So view source and see the code.
[...] apache after reading two most of the popular post of this blog 5 useful url rewriting examples and hide .php extension with url rewriting. How to check weather mod_rewrite module is enabled or not? Well there are lots of technique to [...]
Hi Roshan, great post
Hi:
I created .htaccess file in my htdocs directory.
used:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php[nc]
Also in apache config file used AllowOverride All.
Restaeted the server, still get a 404 page!
I have a registration.php file, but I’d like to be able to browse to registration.htm file , which would display the php file.
Am I missing a step?
thanx
Kamy
Check weather mod_rewrite module is loaded or not..
http://roshanbh.com.np/2008/04/check-enable-mod_rewrite-apache.html
If it’s loaded then check the file name properly..
it should work…
mod_rewrite is enabled and in fact I got your first tutorial to work (Hiding PHP file extension).
Rechecked everything, restarted apache…. still no go
You’ve the file called registration.php file and you’re typing registration.html in browser’s address bar right ? or you’re doing it in reverse way ? can you tell me in detail ??
Here is a successful technique to
eliminate php file extensions in urls.
http://www.easymodrewrite.com/example-extensions
This post is more than hiding file extension…it basically about url rewriting…
Hi Rishan:
I have a file called registration.php and I’m typing registration.html in browser’s address bar right.
I followed your steps to the letter.
Any suggestions?
thnax
kamy…you are typing registration.html in browser but you’re using “.htm” in your rewrite rule not “.html”, I think that is the main problem…
replace the above code with this
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php[nc]
Roshan:
Thank you for reply.
Still does not work!
I checked my phpinfo file, mod-rewrite is loaded.
I’m wondering if I have another problem? Because I tried what Mikey suggested and that did not work on my system either!!
So I’m thinking that something is off/wrong on my system.
As I said your initial tutorial does work here.
VERY CONFUSDED!
:(
Any ideas?
I appreciate the help.
:)
hey kamy…..send me zip file with that .htaccess file and that php file to bhattarairoshan[at]yahoo[dot]com
let me see what is mistake out there
hi Roshan:
thank you for checking my files.
I made the change , restarted Apache, still does not work !!
Is there anything on Apache config I need to change perhaps?
Kamy
Hi kamy…which apache version you’re using? Well, everything seems to be fine in that code…when I check your files there was only one “space” missing and it worked here.
I’ll send you the zipped working file…just unzip and put that folder inside root folder…..hope that will works
Hi Roshan:
Thank you for the files, but the problem STILL ESISTS!!!
This is now becoming more of a puzzle then anything else.
Running Apache 2.2.8 with PHP 5.2.5
My gut feeling is that something in httpd.config is conflicting with the htaccess file.
Any ideas?
Kamy
Well, this is the biggest puzzle I’ve ever faced. That file was perfectly working here…
try removing Options +FollowSymlinks once…
Hi,
I created .htaccess file in my root directory.
**********
IndexIgnore */*
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php[nc]
**********
My index page is php page when i access my site i am getting this error:
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
Am I doing something wrong
Thanks in Advance
-Praveen
It seems that mod_rewrite is not enabled in your apache server. Check this article to see how to check and enable mod_rewrite module….
http://roshanbh.com.np/2008/04/check-enable-mod_rewrite-apache.html
Hi,
Thanks for reply it is working fine now but i have other links on the same page.
When i access them it shows page name+.php extension.
why?
Thanks in Advance
Well, you need to change the links to the “.html” or whatever your rewrite rule is there and for every different kind of url you need to define different rewrite rule
Thanks for ur reply and support.
It is working fine.
-Praveen
Hi,
I have added this rule to hide index.php file(this is my homepage file):
RewriteRule ^index$ index.php [L]
RewriteRule ^about$ about.php [L]
But this rule is not working, it is giving this error when i access this page:
Error 404: NOT FOUND!
Your browser cannot find the document corresponding to the URL you typed in.
I am accessing index page like:
Home
I am accessing about page like:
About Us
This is working fine.
Why it is not working for HomePage?
Any help is appreciated
Thanks in Advance
-Praveen
As I can see there is [L] flag which means end of the rewrite rule…just remove the [L] flag or put it at the last rewrite rule…and it will work
Hi,
when i access homepage i am getting this error
Error 404: NOT FOUND!
Your browser cannot find the document corresponding to the URL you typed in.
I am accessing my homepage
http://www.example.com/index
In .htaccess file i have written:
IndexIgnore */*
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^index$ index.php
RewriteRule ^passenger$ passender.php
RewriteRule ^aboutus$ aboutus.htm
RewriteRule ^contactus$ contactus.htm
Is there anything i need to change?
Thanks in advance
[...] Well, just copy and paste the above code in .htaccess file then the whole website will be redirected to “https” when the browser is opened in “http” mode. The browser just get redirected using url rewriting in .htaccess. [...]
[...] Just copy and paste the above code in .htaccess file then the whole website will be redirected to “https” when the browser is opened in “http” mode. The browser just get redirected using url rewriting in .htaccess. [...]
mod_rewrite is absolutely brilliant. if you know your regular expressions from perl or php they’re easy too!
Hi Roshan,
I tried your example. But its not working. According to someones reply I have to search “AllowOveride All” in httpd.conf file.
But I didn’t found this keyword. So how to make .htacces file working. Please reply me sson.
Byee and have a nice day.
If it’s not here then put the following code in your httpd.conf where you’ve to specify the directoryname in the first line
< directory “D:/xampp/xampp/htdocs” >
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</ directory >
Hi Roshan:
I need a help. i am using htaccess file to rewrite the url’s for a project in my local server. the path to the server is something like this.
ip/projectname/html
all the files are inside html folder and the htaccess file also resides there. i have tried to use it for one pf the page. Below is the content of the htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index_html$ index1.php[nc]
RewriteRule ^faq$ faq.php[nc]
when i see in browser it still shows the extension.
mod_rewrite and other options are enabled.
please help.
How are you accessing that file ? What did you type in browser to access the index1.php or faq.php file ?
Rescrierea url-urilor cu .htaccess…
Converia din URL dinamic in Url static…
Hi Roshan
I have some problem regarding .htaccess,I have to do folder based Url rewriting in my site.I have also tried the 3rd rule that you have written.
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
But when I use it on any page of my site,all the images on that page are not being displayed.
Can you please help me a little to get out of this problem?
yes you might face this…for this you can use “base” html element for absolute path of image and links