homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect all traffic and requests to index file or other page

 9:59 am on Jan 19, 2008 (gmt 0)

Hi, i have quite a website and hope someone wants to take it over.
but as all existing pages are gone i would like that every visitor gets redirected to a page where all is explained.
So meaning,all indexed pages ...so if they come to my site by a google search that they wont see page cannot be found or whatever.
So i guess i need a rewrite or something that i can put in the htaccess that catches all traffic.
How do i do that?



 10:55 am on Jan 19, 2008 (gmt 0)

I'm no htaccess guru or anything, but this seems to work on my server:

RewriteEngine on
RewriteRule ^(.*) /main.html

"(.*)" matches any file that someone tries to access on your server and displays the main.html file instead.

This does not change the url.

You will have to append a [R] flag or something to make it actually redirect the url I think (so it shows /main.html in the Location bar).

Can't find what flag it is at the moment, it's in the documentation or someone else will know for sure.


 6:10 pm on Jan 19, 2008 (gmt 0)

Redirecting or rewriting all URLs to a single page will effectively create massive duplicate content. Best practice is to use a custom 410-Gone error page, and do the explaining on that page. For more info, see this recent thread [webmasterworld.com].



 6:54 pm on Jan 19, 2008 (gmt 0)

thanks,i will give that a try.


 9:46 pm on Jan 20, 2008 (gmt 0)

well i tried this ..

ErrorDocument 410 /path-to-410-error-document.html
RewriteRule .* - [G]

but then i cannot even see my main index.html page anymore.


 10:15 pm on Jan 20, 2008 (gmt 0)

That's because your code returns a 410 for *all* urls, including that of the error document itself. Exclude that document from the rule:

ErrorDocument 410 /path-to-410-error-document.html
RewriteRule !^path-to-410-error-document\.html$ - [G]

This will return 410-Gone for all URLs except the error document itself.

If you wish to add the requirement that the request is referred from google search, then add a RewriteCond:

ErrorDocument 410 /path-to-410-error-document.html
RewriteCond %{HTTP_REFERER} ^http://www\.google\.
RewriteRule !^path-to-410-error-document\.html$ - [G]

(Note that it is up to the user-agent {e.g. browser} to send a referral header. Some don't.)

Want those URLs out of Google's index as well?

ErrorDocument 410 /path-to-410-error-document.html
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5\.0\ \(compatible;\ Googlebot/
RewriteCond %{HTTP_REFERER} ^http://www\.google\.
RewriteRule !^path-to-410-error-document\.html$ - [G]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].



 10:26 pm on Jan 20, 2008 (gmt 0)

Thanks JD , but can i use the main index.html as the error page?
seems logical to me..


 10:32 pm on Jan 20, 2008 (gmt 0)

in the index file a few images are used,they are in the images folder,can i exclude that somehow?


 10:48 pm on Jan 20, 2008 (gmt 0)

situation is this..
i use 1 index.html with some images for the design.
now with the rule only excluding the index.html file the images dont show.
how do i solve that?


 4:10 am on Jan 21, 2008 (gmt 0)

You will need to adjust the RewriteRule and maybe add some more RewriteConds as well.

If you only have few images to include, or they are using the same prefix, or you put them in a special directory, then you only need to adjust the RewriteRule as follows (respond with Gone to everything except path-to-410-error-document.html and anything from the path_to_images directory):

RewriteRule !^(path-to-410-error-document\.html)(path_to_images/.+)$ - [G,L]

or (respond with Gone to everything except path-to-410-error-document.html and any of those files in the path_to_images directory which starts with gone_images_):

RewriteRule !^(path-to-410-error-document\.html)(path_to_images/gone_images_.+)$ - [G,L]

If this looks a bit complicated, or you have many files which does not have the same prefix, or they are in different directories, then you will need to use some RewriteCond's:

RewriteCond %{REQUEST_URI} !^/path-to-410-error-document\.html$ [NC]
RewriteCond %{REQUEST_URI}
!^/images/image_header\.jpg$ [NC]
RewriteCond %{REQUEST_URI}
!^/branding/logo\.jpg$ [NC]
RewriteCond %{REQUEST_URI}
!^/images/sorry_we_are_gone\.jpg$ [NC]
RewriteRule .* - [G,L]

And there's one more way (not counting of the use of the RewriteMap approach), use RewriteRules to stop processing more rules, so you first check the existing resources, but then you deny everything else. This might work a bit faster than the one above.

RewriteRule $path-to-410-error-document\.html$ - [NC,L]
RewriteRule ^images/image_header\.jpg$ - [NC,L]
RewriteRule ^branding/logo\.jpg$ - [NC,L]
RewriteRule ^images/sorry_we_are_gone\.jpg$ - [NC,L]
RewriteRule .* - [G,L]

This might look odd, but all what it says, if the requested resource is for example the path-to-410-error-document.html file, then do nothing, and stop processing more rules. If none of the first four rules do match, then the last rule will match anyway, so the response will be "Gone" again (so your explanation page will be displayed).

And a very last suggestion: Prefix your files with "website_is_gone_" (for example), then the original suggestion of Jim will work, only need to change the RewriteRule to (respond with "Gone" to all request where the url is not www.example.com/website_is_gone_anything or www.example.com/any_directory/website_is_gone_anything):

RewriteRule !(^/)website_is_gone_.+$ - [G]

These are all alternatives, so don't try to use them all at once, choose the one you like, or fits into your situation better, and go for that one. :-)

[note]You will need to change the broken pipe () characters with the solid ones before using any of the rules above, otherwise you will get errors from your Apache box.[/note]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved