Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite 301 via .htaccess

301 redirect all html pages below root

         

Angua

8:39 am on Nov 24, 2004 (gmt 0)

10+ Year Member



Q. I would like to know how I can permanently redirect all html page requests below root (excluding the root index page of siteA) to siteB having the identical html page names?

EX: User types:
[siteA.com...]

Gets redirected to:
[siteB.com...]

The only exception is that [siteA.com...] (remains in use)

Would this be correct?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?siteA\.com\/index\.html\/
RewriteRule (.*) [siteB.com...] [R=301,L]

#my current setup is Apache 1.3. Each site has a unique IP. Both are located on the same server(I think).
--------------------

jdMorgan

6:13 pm on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Angua,

Welcome to WebmasterWorld!

You'll need to add index.html (and probably "/") as an exclusion to your rule. You cannot include this path information in a RewriteCond testing HTTP_HOST, since the path is not part of the Hostname. So, you'll need a separate path exclusion test.

Also, in mod_rewrite, there is no need to escape slash characters. This is needed in PERL and PHP because of the syntax of string-matching directives, but is not needed in mod_rewrite.

Lastly, your Rule would have redirected all pages of siteA to "index.html" on siteB, which is not what you said you wanted.

Putting all that together, we get:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?siteA\.com
RewriteCond %{REQUEST_URI} !^/(index\.html)?$
RewriteRule (.*) http://www.siteB.com/$1 [R=301,L]

Do not end-anchor the domain name in the first RewriteCond; doing so will cause the Cond to fail if a port number is appended, as in "www.siteA.com:80/index.html".

You may need to add Options +FollowSymLinks at the top to enable mod_rewrite. This depends on your server configuration, and should only be added if none of your RewriteRules appear to work. You should get a 500-Server Error if this is the case.

Jim

Angua

11:29 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



The solution works well. Thank you and thank you again for the welcoming.

Resulting issue:
The mod_rewrite solution below does not let me use images for the index page of SiteA (either in root or /image/ ).
How would I define a rule to enable use .gif and .jpeg images located in www.siteA.com/image/ folder for index.html(root)?

?ex: RewriteCond \.(gif¦jpg)$ /image/
?ex: RewriteRule \.(gif¦jpg)$ /image/

My current .htaccess file:
#----------- mod_rewrite 301 -------------
#Forward all user requests below root(index.html)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?siteA\.com
RewriteCond %{REQUEST_URI}!^/(index\.html)?$
RewriteRule (.*) [siteB.com...] [R=301,L]

<LIMIT GET POST>
order deny,allow
allow from all
</LIMIT>
options -indexes

#-------------------------------------------

jdMorgan

12:20 am on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no easy solution to that.

I'd suggest you move the images used by siteA/index.html into a separate directory and update the image links on siteA/index.html to refer to that new image directory. Then you can add another exclusion to your rule.

For example, change all image links on siteA/index.html to refer to /imagesA/xyz.gif
Then add


RewriteCond %{REQUEST_URI} !^/imagesA/

to your ruleset.

The problem is that the server does not "know" what page is requesting an image; Each HTTP request is separate, and the server does not track the state of a page being rendered by the client. So the server does not know that it is index.html requesting the image. You could use HTTP_REFERER, but that is notoriously unreliable. The method suggested above is the only one that guarantees that the correct images will be loaded for pages on both siteA and siteB.

Jim

OutElf

6:16 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



I'd like to recessitate this thread - I have the same question, only I can't really figure out the mod_rewrites you suggested here.

What I want is to "rewrite" all requests for a folder filled with only images to a completely different server... Could someone give me a hint?

jdMorgan

11:12 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OutElf,

Welcome to WebmasterWorld!

You can't "rewrite" to a different server, since rewrites are internal to the server by definition.

You can, however, redirect to another server. The easiest way to do that is to use the RedirectMatch [httpd.apache.org] directive of Apache mod_alias. You could also use mod_rewrite, but it's more complicated to use if you haven't done so before.

When you use a redirect, be sure to specify whether you want a permanent or a temporary redirect. If you use a temporary redirect, that means you intend to remove the redirect at some time in the (relatively near) future. A permanant redirect means that the resources have been permanently moved to the new URL, and that they won't be back. In this case, user-agents such as search engine robots will delete the old URL from their records and use the new URL instead. Web browsers could conceivably also do this to update user bookmarks, but I don't know of any that do. Some add-on boobmark managers do have this capability, though.

Jim

OutElf

11:34 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



Thanks for your swift reply

I'll have a good read tonight and see if I can make it happen - otherwise I'll get back to you ;)