1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Rewrite index.* to /

Discussion in 'Search Engine Optimization' started by nevetS, Jan 4, 2006.

  1. #1
    Took me a bit to figure this out, so I thought I'd share for the next guy.

    Basically, the idea is to map common duplicate URLs to a single URL. I've seen rumors that this can affect your PR positively - you may see different pagerank if you go to http://yourdomain.com, http://www.yourdomain.com, or http://www.yourdomain.com/index.htm - but in my case, I have the same PR at all three, so I don't know if I'm solving a problem that no longer exists - but the side benefit is that your users always see the same url when they go to your site, which is just plain good for consistency reasons. It also allows me to change my default page from index.htm to index.html to index.php in the future without affecting the ranking or inclusion of my index pages.

    Of course, this assumes you are using apache, have mod_rewrite installed, and your host has given you Options FileInfo in your directory declaration in your httpd.conf file. If this doesn't work for you, you probably are lacking in one of these requirements.

    Here's a few examples:
    http://yourdomain.com
    http://yourdomain.com/index.html
    http://yourdomain.com/index.htm
    http://yourdomain.com/index.php
    http://www.yourdomain.com/index.php
    http://www.yourdomain.com/index.html
    all redirect to:
    http://www.yourdomain.com/

    http://yourdomain.com/subdir
    http://yourdomain.com/subdir/index.html
    http://yourdomain.com/subdir/index.htm
    http://yourdomain.com/subdir/index.php
    http://www.yourdomain.com/subdir/index.php
    http://www.yourdomain.com/subdir/index.html
    all redirect to:
    http://www.yourdomain.com/subdir/

    Here's an example with some comments. The comments begin with ## and those lines can be removed without affecting anything. They're just their so that you know what the lines do in the future, can understand my thinking in putting this together, and so that someone new to mod_rewrite can understand what's going on.

    create a .htaccess file in your root directory (or insert this in your existing one)
    
    ## The basics to get mod_rewrite going ##
    RewriteEngine on
    RewriteBase /
    ## rewrite the index file in your root directory ##
    ## this should work, regardless if you have index.html, index.htm, 
    ## or index.php
    RewriteRule ^index\.(htm|html|php) http://[COLOR=Red]www.yourdomain.com[/COLOR]/ [R=301,L]
    ## rewrite the index file in subdirectories
    RewriteRule ^(.*)/index\.(htm|html|php) http://[COLOR=Red]www.yourdomain.com[/COLOR]/$1/ [R=301,L]
    
    ## match condition: skip any www requests 
    ##  or another way of saying this - match anything that doesn't start
    ##  with www
    RewriteCond %{HTTP_HOST} !^www\..*
    ##  change any requests to [COLOR=Red]www.yourdomain.com [/COLOR]
    ##   combined with the above RewriteCond means any
    ##   non-www requests are redirected to www.yourdomain.com
    ##   the %{REQUEST_URI} appends whatever url to [COLOR=Red]www.yourdomain.com[/COLOR]
    RewriteRule ^.*$     http://[COLOR=Red]www.yourdomain.com[/COLOR]%{REQUEST_URI} [R=permanent,L]
    
    Code (markup):
    I did find this other useful and similar .htaccess/mod_rewrite post:
    http://forums.digitalpoint.com/showthread.php?t=33317
     
    nevetS, Jan 4, 2006 IP
    Seiya likes this.
  2. guitarlp

    guitarlp Peon

    Messages:
    8
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I actually just figured how to do this on a windows machine running ISAPI_REWRITE. If you're in my shoes and running the above, you can use this code:

    RewriteCond Host: (.*)
    RewriteRule /index\.htm http\://$1/ [I,RP]
     
    guitarlp, Jan 4, 2006 IP
    nevetS likes this.
  3. SpiderDog

    SpiderDog Guest

    Best Answers:
    0
    #3
    Could you give us some links that will help understand what ISAPI_REWRITE is all about? How and when is it used? I assume it runs on your host server machine since that is where the .htaccess file is. Will it work if that server is running Linux? Thanks.
     
    SpiderDog, Feb 11, 2006 IP
  4. Seiya

    Seiya Peon

    Messages:
    4,666
    Likes Received:
    404
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the post used this code to put my site back to www. only, i took it a while back because i tought it was the reason my coop weight down but thats was a wrong assumption ;p
     
    Seiya, Feb 11, 2006 IP
  5. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #5
    ISAPI Rewrite is for IIS servers. I don't know a lot about it myself, but my understanding is that it is a module similar to apache's mod_rewrite.

    I couldn't do any better than google to give you information, but I'd search the terms ISAPI Rewrite and IIS rewrite and IIS mod_rewrite. In fact, if you did a site search here at DP for those terms, you probably will get some hits.
     
    nevetS, Feb 11, 2006 IP
  6. asterix001

    asterix001 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks so much for this! This is also exactly what I've been looking for. However, I am having trouble getting it to work in Drupal with "Clean URL's" turned on. Here's the statements Drupal adds to .htaccess to generate "Clean URL's":

    # Rewrite current-style URLs of the form 'index.php?q=x'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    Then I put your code after this, with the following results: the first two Rewrite rules work in that they redirect index.php to the root domain, however, somehow this also turns off the "clean URL's" (ie. they now show up in the form /?q=node instead of simply /node.).

    Finally, the last two lines, which serve to "send everything to www" cause an infinitely occurring loop - so that the URL shows domain/domain/domain. I have tried delving in to the RE's of Rewrite rules but am having a harder time than I anticipated. I'm guessing the Drupal RewriteRule is conflicting with yours?

    Can anyone help?

    Thanks for your time!
    Alex
     
    asterix001, Mar 31, 2006 IP