Forum Moderators: phranque
Kind of an odd one this. I was looking to see if there was a way to conditionally redirect. That is redirect only if the remote file exists.
I realise this might generate a response overhead but it may be justified in this case.
domain1.com moved to domain2.com
domain1.com has error handling/tracking and filters to stop negative traffic.
Requests for pages that exist domain2.com made on domain1.com should be redirected to domain2.com, otherwise the request should be allowed to be processed by domain1.com htaccess rules/filtering/etc.
on domain1.com htaccess I could write a rule such as:
RewriteRule ^page.html$ [domain2.com...] [NC,L,R=301]
But this would require me updating a changing list of pages.
I could write:
RewriteRule ^(.*)$ [domain2.com...] [NC,L,R=301]
but this would negate a lot of the custom filtering and error tracking on domain1.com
So I need a rule that checks if the file returns 200 header on teh remote domain and if so then redirects.
Is this feasible? Is there a better way?
Matt
If they're not in the same filesystem, then you'll need a script that can do HTTP requests to actually go fetch the URL and check the response header. This will be highly inefficient, since every domain2 page will be fetched twice, and it will also make a mess of the logs and "stats" on domain2.
Jim
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain1.com.*$ [NC]
RewriteCond %{REQUEST_URI}!-U
RewriteRule ^(.+) http://domain1.com/$1
then on domain1.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER}!^http://(www\.)?domain2.com.*$ [NC]
RewriteRule ^(.+) http://domain2.com/$1
Something like that so domain1.com only redirects to domain2.com if it hasn't already tried that. Got the idea from askapache .coms ultimate htaccess article.
I will rethink how I approach it I feel. If it can't be done by htaccess alone I'm not going to bring in a script to complicate matters.
HTTP_REFERRER is fine but has caused me problems in the past with items like Norton that tend to blank it.
What does:
RewriteCond %{REQUEST_URI}!-U
do? I've never seen -U before.
I suspect I'll give up on the filtering in domain1 and just generically redirect to domain2.
Something like:
RewriteRule ^(.*)$ [domain2.com...] [L,R=301]
As you may have guessed I had my site on domain1.co.uk and domain2.com. This lead to SE duplication penalties. I settled on domain2.com and want to transfer and SE visits on domain1.co.uk to domain2.com, if possible whilst filtering out malicious traffic load from hitting domain2.com. I'll live with the above instead.
Matt
Remove the files / pages / directories you want to redirect from domain1 and upload them to domain2. Then check to see if the requested resource (file) exists on domain1.
If the resource being requested from domain1.com DOES NOT EXIST, redirect to domain2.com otherwise serve the information from domain1.
Justin