How to remove index.php from url using .htaccess (mod_rewrite)

htaccess-tricks-tips

For better SEO optimization and make urls more search engine friendly , remove index.php from URL and make it easier to read.

Remove index.php from URL can be done by writing only two lines in your .htaccess(mod_rewrite in apache) file. Before writing this rule in .htaccess , make sure that your mod_rewrite is enabled(On) in your apache server. Most probably mod_rewrite is enabled in Linux server but for windows server , you need to contact hosting people to make mod_rewrite enabled. You can check this by looking in phpinfo().

Below is the Rules which will remove index.php from URL using .htaccess. Look at below links for .htaccess rules.

 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/ RewriteRule ^(([^/]+/)*)index\.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]

Redirect 301 means Moved Permanently so most search engines will remove index.php from URL.

To know more about programming,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.

About Rakshit Patel
I am PHP Freelancer, Rakshit Patel AKA WordPress Freelancer working as PHP Programmer India. I am here to share all my developing knowledge, quick coding techniques, quick solutions to save your time and help you guys for any issue or bug fixing. If you have any PHP, WordPress, Magento related work, please contact me. I am interested for the work.

24 Comments on How to remove index.php from url using .htaccess (mod_rewrite)

  • shail
    December 25, 2009 at 5:28 am

    Yes it will be helpful for SEO purpose as it will remove index.php from URL.
    It helps me as i was in search of this .htaccess rules. I found so many solutions in google but none of them works for me.
    This works for me. Thanks.

  • shail
    December 25, 2009 at 10:58 am

    Yes it will be helpful for SEO purpose as it will remove index.php from URL.
    It helps me as i was in search of this .htaccess rules. I found so many solutions in google but none of them works for me.
    This works for me. Thanks.

  • abril
    January 31, 2010 at 4:28 am

    I was hopeful, but this code gives me a server error.
    Thanks for posting, though.

  • abril
    January 31, 2010 at 9:58 am

    I was hopeful, but this code gives me a server error.
    Thanks for posting, though.

  • Rakshit Patel
    February 1, 2010 at 5:18 am

    @abril – It works for me.. are you getting 500 server error ?

  • Rakshit Patel
    February 1, 2010 at 10:48 am

    @abril – It works for me.. are you getting 500 server error ?

  • SECUDE IT Security Standard
    February 9, 2010 at 4:44 am

    Yeah i'm getting Internal Server error and still find to way to fix that, It's so strange because that code is look fine …

  • SECUDE IT Security Standard
    February 9, 2010 at 10:14 am

    Yeah i'm getting Internal Server error and still find to way to fix that, It's so strange because that code is look fine …

  • Jal Ulati
    March 18, 2010 at 1:39 pm

    I too received the 500 server error.
    This is what I want the .htaccess file to do:
    > When some types http://www.thiswebsite.co.uk/index.php into their browser, the browser DOES execute the index.php page, but is DOES NOT show the "index.php" in the address bar.

    Now, is this actually possible? I've searched long and hard, and every 'solution' I've found just breaks my site. What am I doing wrong? Am I confusing "remove index.php" with "hide index.php"? I do NOT want to avoid the index.php file as it controls my site, I simply want to hide the words "index.php" from the address bar.
     
    Thanks.

  • Jal Ulati
    March 18, 2010 at 7:09 pm

    I too received the 500 server error.
    This is what I want the .htaccess file to do:
    > When some types http://www.thiswebsite.co.uk/index.php into their browser, the browser DOES execute the index.php page, but is DOES NOT show the "index.php" in the address bar.

    Now, is this actually possible? I've searched long and hard, and every 'solution' I've found just breaks my site. What am I doing wrong? Am I confusing "remove index.php" with "hide index.php"? I do NOT want to avoid the index.php file as it controls my site, I simply want to hide the words "index.php" from the address bar.
     
    Thanks.

  • Stewart
    June 21, 2010 at 7:22 am

    Hey Jal.. I would be interested to know -did you manage to sort out your index.php problem? I find myself in exactly the same situation as you, nothing seems to work for me either..still looking.

  • Stewart
    June 21, 2010 at 12:52 pm

    Hey Jal.. I would be interested to know -did you manage to sort out your index.php problem? I find myself in exactly the same situation as you, nothing seems to work for me either..still looking.

  • irena
    August 18, 2011 at 7:40 am

    If you guys haven’t solved the problem (only 1 year passed by since you asked the last question, but this can be useful to someone else :) ), try adding this in your .htaccess file.

    # RewriteBase /

    # Redirect index.php Requests
    # ——————————
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    RewriteCond %{THE_REQUEST} ^GET

    # Standard ExpressionEngine Rewrite
    # ——————————
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    #
    ## End – Joomla! core SEF Section.

  • Andy
    September 19, 2011 at 8:06 am

    Thanks Irena,

    it work find for my site i use below code:

    # Redirect index.php Requests
    # ——————————
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    RewriteCond %{THE_REQUEST} ^GET

  • Irena
    September 20, 2011 at 2:13 am

    No problem :))

  • Alice
    July 21, 2012 at 10:53 pm

    Thank you for this. Really helped me out after trying to write this on my own and failing horribly.

  • best infrared heater
    July 11, 2013 at 6:41 am

    I was curious if you ever thought of changing the structure of your
    blog? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect
    with it better. Youve got an awful lot of text for
    only having one or 2 images. Maybe you could space it out better?

  • SEO consultant
    October 9, 2014 at 12:50 pm

    Hello are using WordPress for your blog platform?

    I’m new to the blog world but I’m trying to get started and create
    my own. Do you need any coding knowledge to make your own blog?
    Any help would be greatly appreciated!

  • seo absprungrate
    October 9, 2014 at 3:08 pm

    It’s remarkable for me to have a website, which is helpful for my experience.
    thanks admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Sponsors

Sponsors

Recommended

DHgate Computers
DHgate.com - Computers, Cell Phones & more
  • We find everbuying built in php and use css to control the style.

About Author

PHP Freelancer

PHP Freelancer I am PHP Freelancer, Rakshit Patel working as PHP Programmer India. I am here to share all my developing knowledge with you people. For Hire PHP Developer India, contact me.
Follow on twitter: @raxit4u2

Get Free Programming Tutorials:

Become a Fan

Popular Topics

Recent Posts

Most Popular

Recent Comments

Get a Free Quote
$
$
$
Security Question: 3 + 8 = ?
$
 
Listing all pages