Forum Moderators: phranque
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).
--------------------
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]
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
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
#-------------------------------------------
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/
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
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