FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Redirect index.php to root

by 22 comments

WordPress blogs show the same duplicate content for http://www.askapache.com/index.php and http://www.askapache.com/. If you've read about using a robots.txt file for WordPress SEO, than you already understand this setup results in Duplicate Content penalties being levied against your Blog and Web Site by Search Engines.

A common request is to be able to remove or redirect the index.php from appearing in the url. This is possible only with server-side technology like .htaccess configuration files or your main server config by using the Mod_Rewrite Rewrite Module.


The Redirect Fix

The fix is a bit of clever .htaccess code utilizing mod_rewrite to only redirect index.php if the request for /index.php came from a client (e.g. browser or web robot), and not if the request is an internal redirect that apache does when / is requested so that it can serve the contents of index.php.

.htaccess mod_rewrite code

askapache.com/index.php

Options +FollowSymLinks
DirectoryIndex index.php
 
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php HTTP/
RewriteRule ^index.php$ http://www.askapache.com/ [R=301,L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

askapache.com/blog/index.php

Options +FollowSymLinks
DirectoryIndex index.php
 
RewriteEngine On
RewriteBase /blog/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /blog/index.php HTTP/
RewriteRule ^index.php$ http://www.askapache.com/blog/ [R=301,L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

More Info

The problem with this is that any request for "/" is internally rewritten to index.php by Apache mod_dir if you have index.php in the DirectoryIndex list (the usual set-up), so it's difficult to avoid an infinite loop.

Tags

August 9th, 2007

Comments Welcome

  • Dave

    This code works like a charm. ONE PROBLEM: Now my 404 pages don't seem to work. If I go to my root and type a page that doesn't exist, sdfsdfwe.php, the browser acts like that page exists and serves my index instead.

    Is there a way to keep my 404 working?

  • http://www.askapache.com/ AskApache
  • http://clubber.com.ar Mato

    Hello im trying and trying something but i cant!

    First i need to say that i have a rule on .htaccess on my root folder with the code:

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} www.mysite.com.ar [nc]
    rewriterule ^(.*)$ http://mysite.com.ar/$1 [r=301,nc]

    I say it because maybe it modify the new thing im trying to do.

    I need a redirect from:
    mysite.com.ar/forum/ --> mysite.com.ar/forum/index.php

    Is this possible?

    Thanks in advance

  • Ellimist

    Thanks for posting this. I've been looking for this a long time.

    However, I'm having one problem : Any random, non-existing directory-url, like site.com/something/ is giving a 200 Found response code now. And it's loading the index page.

    A copy of my .htaccess file can be found here.

    I'd be grateful for any advice.

  • Andrewsha

    Hello!
    Working as a charm!
    But how to rewrite mysite.com/index.php to mysite.com ?

  • mktanny

    hey i am having the same problem as Dave

    Regards

  • Jal Ulati

    @ Andrewsha

    Firstly, I'm not criticising, I'm simply confused: what works like a charm?

    I had thought that the original solution would answer your question "But how to rewrite mysite.com/index.php to mysite.com?"

    My question is also how to simple re-write, NOT re-direct, from www.mysite.com/index.php to simple www.mysite.com.

    Can anyone help us?

  • http://www.averymedia.com Brian

    I am trying to re-direct index.php. and still be able to use my client login features (plugin on home page) on Joomla. Successful to get all redirects except that now my client login plugin does not work when I redirected the index.php. Tried using the ... RewriteRule . /index.php [L] ... but same problem. So for now it's back to the site without index.php redirect until I can find an answer. Apache .htaccess code for this or not? I have searched and searched for an answer - still looking. Anyone?

    ~~Brian

  • http://www.askapache.com/ AskApache

    @Brian

    I don't think there is such a thing as something that can't be done with mod_rewrite. That is an unusually difficult situation though.. If you were my client, I would make a list of what is different in the REQUESTS for joomla long vs. everything else. Then I would be able to find the correct RewriteCond's using the mod_rewrite cheatsheet.

  • http://www.coolchecks.net/ Sherry

    I used this code and fixed the problem with seeing the index.php in the URL.

    So my question is about how Google sees these links. There may be people
    linking to any combination of these links:

    http://www.domain.com/index.html
    http://www.domain.com/
    http://www.domain.com
    http://www.domain.com/index.php

    With your script, does that mean that everyone redirects to http://www.domain.com/
    and does that help with having all backlinks go to one URL? I've heard that having the
    4 - can dilute the backlinks.

    Thanks!

  • http://www.steevenz.com Steeven

    I want to redirect browse folder into the root index.php through .htaccess is that possible?
    example:
    the request url is = http://www.site.com/foldername
    this request has to be not to browse the folder, but back into http://www.site.com/index.php and will be process there.. as information the folder with the foldername is exist on the server that cause when requesting is browse the folder..

    thanks

  • julio

    Hello. I have a magento store and I'm trying to delete or redirect links that have index.php. Example:

    http://domain.com/my-film-color-guide
    http://domain.com/index.php/my-film-color-guide

    (I want to automatically redirect this to the other link w/o index.php.

    any way I can do this via .htaccess?

    Thank you very much.

  • http://www.vegascroupier.com/ Amburo

    Superb article, bless you!

  • http://barcoderesourcing.com Barcode Repair

    Thanks for the awesome tip... That index.php thing is easy to miss with Wordpress and other packages out there such as zen cart... This is an easy fix... Just popped your code into my htaccess file and all good now... Thanks.

  • http://www.goolsearch.com/ gool

    thanks ,you made my day..redirecting working now

  • http://www.miscarchives.com knyri

    Following is a better way to do it and it includes all possibilities

    #######################################
    # Redir to folder if using index file #
    #######################################
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /(index)|(home)(..{3,4})?$ [NC]
    RewriteRule ^([^/]+/)+ http://www.askapache.com/$1? [R=301,L]
     
    ##########################################################
    # Local Rewrite to File. Replace php with your extension #
    ##########################################################
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #next line prevents a 404 becoming a 500
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [L]
  • http://www.askapache.com/htaccess/redirect-index-blog-root.html Driver

    knyri in my case index.php still remains in the url...

  • http://erichschiffmann.com Arthur Klein

    in this rule:

    RewriteRule ^index.php$ - [L]

    What exactly do the following control?

    " ^"      =
    ""       =
    "$ -"    =
    "[L]"    =

    Thank you for your help
    Arthur

    RewriteRule ^index.php$ - [L]
  • http://www.dmsoftware.it Davide Masserini

    Great! It worked perfectly! Thanks so much!

  • Badruddin

    Thanks you so much, you doing wonderfull sharing and support.

  • http://URL Nameg1smd

    First example in the article is for use in htaccess file in site root.

    Second example in the article is for use in htaccess file in /blog/ folder.

    Please make that more clear.

    There's a third bit of code you should include - for when the index.php file is in the /blog/ folder and the redirect code is placed in the htaccess file in the site root.

    Those are the three possible site configurations.

    Code seen in the comments...

    RewriteCond %{REQUEST_URI} /(index)|(home)(..{3,4})?$ [NC]
    RewriteRule ^([^/]+/)+ http://www.askapache.com/$1? [R=301,L]

    There are multiple problems with the above code. Do not use it. Condition runs for every request, you should also include the index|home test in the Rule pattern for better efficiency. In the condition, repeat the same pattern with opening anchor and matching for folder levels. No extension or closing anchor specified for index, only for home. Unnecessary brackets, or brackets in wrong place.

    RewriteRule ^(.*)$ $1.php [L]

    Add a slash before the $1 otherwise you leave your server open to path injection hacks.

    RewriteCond %{HTTP_HOST} www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]

    The above code fails to redirect many non-canonical URLs, such as www request that include port number, etc. Try:

    RewriteCond  %{HTTP_HOST}  !^(www.example.com)?$
    RewriteRule  (.*)  http://www.example.com/$1 [R=301,L]
  • http://www.bunnaya.com/ Bunnaya Motivate

    Thanks for the post. looking for simple code to understand for a noob like me and now i find this

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter



Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+ | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain