Redirecting from index.html to root

Samg86

New member
Aug 2, 2009
16
0
0
Hi all. I've been trying to set up a .htaccess file today in order to do the following:

1) Link to my custom 404 page
2) Stop people hotlinking
3) Redirect non-www requests to the www equivalent
4) Redirect /index.html requests to the root

1, 2 and 3 were fine. 4 isn't working for some reason, and I can't figure out why. Can anyone have a quick look at my code and see if i'm making any stupid mistakes? I'm a bit of a noob so hopefully its glaringly obvious.

My .htaccess file is:

Options +FollowSymlinks

#1
ErrorDocument 404 /custom404.html

#2
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mysite.co.uk/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]

#3
RewriteCond %{HTTP_HOST} !^www.mysite.co.uk$
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [R=301]

#4
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.mysite.co.uk/$1 [R=301,L]
and the file is placed in the same location as my index.html file.

Any help would be much appreciated.

Thanks
 


I do that with a 301 in the php header as below.

I call a function as below:

PHP:
fix_index_url();

Then this is the function, which obviously needs including in the index.

PHP:
// redirect requests to index.php and index.html to the root
function fix_index_url()
{
  // if the request is for index.php we redirect to ./
  if (preg_match('#(.*)index\.(html|php)$#', $_SERVER['REQUEST_URI'], $captures))
  {
    // perform a 301 redirect to the new URL
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $captures[1]);
  }
}
 
Hey, I tried changing it to just

Code:
RewriteRule ^index\.html$ http://www.mysite.co.uk/ [R=301,L]

And it worked for me.
 
Try this as well if you want to do the redirecting for pages deeper in your url structure:

Code:
# Redirect aliases of directory index pages to the containing directory.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)index\.(php|html|htm)$ /$1 [R=301,L]