Forum Moderators: phranque

Message Too Old, No Replies

Redirection/Rewriting problem

         

xt35

12:36 pm on Nov 1, 2009 (gmt 0)

10+ Year Member



Hello,

I have a domain (let's name it MyDomain.com) that I want to be redirected to a directory on another domain (by example MyNewDomain.com/MyDirectory). That means all the requests to MyDomain.com should be redirected to that directory on the new domain.

But I need an exception. I need a rewriting when a specific page is requested on MyDomain.com. By example, when the users try to visit http://www.MyDomain.com/download/2008/files.php (page that doesn't exist) I want to send them to a specific existing page, by example http://www.MyDomain.com/download.html

I know the way I can make both redirections, but I fail to put them together.

For the domain redirection, I can use something like:

RewriteBase /
RewriteRule .? http://www.MyNewDomain.com/MyDirectory [R=301,L]

For the page redirection, I can use something like:

RewriteBase /
RewriteRule ^download/2008/files\.php$ download.html [R=301,L]

The problem is the 2nd RewriteRule isn't working if the 1st RewriteRule exists. Please help.

Thank you,
xt

[edited by: jdMorgan at 3:33 pm (utc) on Nov. 1, 2009]

jdMorgan

2:31 pm on Nov 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you redirecting because you want or need to externally redirect, because these sites are hosted separately, or because you're not familiar with internally rewriting and the difference between that and externally redirecting?

It's puzzling that you would want to pay for two domains but then redirect one to the other, exposing the target domain in the visitor's address bar, and essentially discarding any advantage of paying for and maintaining the redirected domain, since search engines will list the redirect target domain if they always see a 301-Moved Permanently redirect on the redirected domain.

Without knowing that, all I can say is that your rules will work better if you follow this guideline: Put your rules in order with external redirects first, ordered from most-specific patterns and conditions (fewest URLs affected) to least-specific patterns and conditions (more URLs affected), followed by your internal rewrites, again in order from most-specific patterns and conditions to least-specific patterns and conditions, and always use the [L] flag unless you know and understand a very good reason not to. IOW, your rules are currently ordered backwards.

However, even after fixing that, these rules may still fail if these domains are co-hosted because you are not testing the requested hostname (the %{HTTP_HOST} variable) with a RewriteCond.

Also note that "RewriteBase /" is the default, and therefore unnecessary unless you've previously set it to a non-default value, and wish to un-do that action for this section of code. Otherwise, RewriteBase is needed only once, or not at all.

Jim

xt35

2:54 pm on Nov 1, 2009 (gmt 0)

10+ Year Member



Thank you for the reply, Jim.

The sites are hosted separately. Yes, I know the disadvantages of the redirecting, but I have no choice. In fact, to tell you more, I have few bots targeting http://www.MyDomain.com/download/2008/files.php and I just want to redirect them to a blank page ( http://www.MyDomain.com/download.html )

Yes, I have put the redirection rules in the correct order, but it doesn't work. For an unknown reason, the redirection to http://www.MyDomain.com/download.html fails everytime and I'm redirected to the 2nd domain.

Do you see anything wrong below:

RewriteBase /
RewriteRule ^download/2008/files\.php$ download.html [R=301,L]
RewriteRule .? http://www.MyNewDomain.com/MyDirectory [R=301,L]

The "download.html" file exists but whenever I go to http://www.MyDomain.com/download/2008/files.php I am redirected to http://www.MyNewDomain.com/MyDirectory , like the 1st rule wouldn't be there.

Thanks,
xt

[edited by: jdMorgan at 3:32 pm (utc) on Nov. 1, 2009]

jdMorgan

3:29 pm on Nov 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add an exclusion for the "download.html" URL. Doing that and correcting what was already there gives:

# Restore RewriteBase to default value
RewriteBase /
#
# Redirect download/2008/files\.php requests to download.html
RewriteRule ^download/2008/files\.php$ http://www.MyDomain.com/download.html [R=301,L]
#
# Redirect all but download requests to new domain
RewriteCond $1 !^download\.html$
RewriteRule ^(.*)$ http://www.MyNewDomain.com/MyDirectory/$1 [R=301,L]

Jim

xt35

4:46 pm on Nov 1, 2009 (gmt 0)

10+ Year Member



Awesome, that exclusion did the job! Thank you :)

xt