Forum Moderators: phranque

Message Too Old, No Replies

Trying to redirect all pages on site to /index.html - but it's looping

Trying to figure out how to exclude /index.html so it won't loop

         

FoundSoul

8:19 pm on Jan 21, 2011 (gmt 0)

10+ Year Member



I'm trying to redirect all attempts to access a domain name, to the index.html file at the root of the same domain. When I do that, of course it redirects again, and again, and again, in a loop.

What I need to do is somehow exclude index.html from the redirect, so that once a user is on this page it doesn't try to redirect them again.

Here's what I've got now, which loops.


RewriteEngine on
RewriteRule .? http://www.mydomain.com/index.html [R=301,L]


I've searched around and tried a lot of things I've found around the web, some from this site, different RewriteCond's and such, and haven't succeeded yet. I'm sure I'm just having trouble with the syntax. Can someone lend a hand?

Thanks!

Jerry Hoffmann
DIYAutoTune.com

g1smd

8:31 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is bad form to redirect TO a named index page.

Redirect to the root URL with trailing slash.

DirectoryIndex index.html
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js|txt)$
RewriteRule .+ http://www.mydomain.com/ [R=301,L]


Change the ? to a + so that only requests for /<something> are redirected.

Exclude requests for images, stylesheets, scripts, and robots.txt from being redirected. Serve a 404 for those if they do not exist.

FoundSoul

8:50 pm on Jan 21, 2011 (gmt 0)

10+ Year Member



Thank you. That's working great!

FoundSoul

9:17 pm on Jan 21, 2011 (gmt 0)

10+ Year Member



FYI I use MS Expression Web to edit some of my sites, and the above code seems to break the ability for this editor to connect and view pages on the site. Not sure if there is an easy way to fix this or not. Doesn't matter too much for what I'm doing on this site, just bought an old out-of-business competitors domain and I'm notifying the user they are out of business on index.html, and then redirecting them to my site so I'll probably never update the index.html again.

If there's an easy fix though I'd implement.

Message I get in MSEW is

301 Moved Permanently
Moved Permanently The document has moved here. Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.7a mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.sparkandfuel.com Port 80

g1smd

9:41 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you requested all URLs to be redirected to the root, and that is what the new code does (for anything that isn't an image, stylesheet or script).

Which URLs do you not want to be redirected?

Alternatively, there's some extra code you can add such that only YOU, or only your SOFTWARE, can access the site and all other requests are redirected.

Before altering any code, you need to define exactly what you actually want. That is, "exactly" what you want.