Rewrite index.* to / using htaccess sitewide

Discussion in 'Apache' started by riospace, Aug 16, 2012.

  1. #1
    This is all I want to do, nothing more - using .htaccess:

    Here's a few examples:
    http://mydomain.com 
    http://mydomain.com/index.html
    http://mydomain.com/index.htm 
    http://mydomain.com/index.php
    http://www.mydomain.com/index.php
    http://www.mydomain.com/index.html
    http://www.mydomain.com/index.htm
    all redirect to:
    http://www.mydomain.com/
    
    http://mydomain.com/subdir
    http://mydomain.com/subdir/index.html
    http://mydomain.com/subdir/index.htm 
    http://mydomain.com/subdir/index.php
    http://www.mydomain.com/subdir/index.php
    http://www.mydomain.com/subdir/index.html
    http://www.mydomain.com/subdir/index.htm
    all redirect to:
    http://www.mydomain.com/subdir/
    Code (markup):


    I am just looking for the best code to do this and I have seen it done a bunch of different ways in this forum with many differing suggestions. I can not seem to understand the specifics and I need to get it right as I have changed my entire site to point to / only and I am waiting to upload until I get this right. Thanks!

    This is what I currently have:

    RewriteEngine on
    
    # Redirect index in any directory to root of that directory
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.[a-z0-9]+)?[^\ ]*\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.website.com/$1? [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^website\.com$ [NC] 
    RewriteRule ^(.*)$ http://www\.website\.com/$1 [R=301,L]
    Code (markup):

     
    riospace, Aug 16, 2012 IP
  2. Ray Baron

    Ray Baron Member

    Messages:
    148
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #2
    If I understand what you want correctly, it is a simple two-liner for index.php and a two-liner for index.html:


    RewriteCond %{THE_REQUEST} /index\.php
    RewriteRule (.*)index\.php$ /$1 [R=301]
     
    Ray Baron, Aug 16, 2012 IP