Forum Moderators: phranque

Message Too Old, No Replies

old-domain.com/folder to new-domain.com/folder 301 redirect

         

finaltransit

10:58 pm on Dec 27, 2011 (gmt 0)

10+ Year Member



Hello,
For last few years, I hosted a wp blog at old-domain.com/folderX. Recently, I purchased new-domain.com and I am using the same wp installation at the same location (using the same db, etc.) on my server.

I could get my blog to run smoothly for the past week from new-domain.com/folderX by changing wp variables.

However, since I have several incoming links from external websites, I am trying to redirect all "old-domain.com/folderX" traffic to "new-domain.com/folderX", but I cannot do it.

I tried numerous techniques, for e.g.:

1. changing .htaccess in root folder:
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^folderX$ http:/new-domain.com/folderX? [R=301,L]

2. changing .htaccess in /folderX:
RewriteCond %{HTTP_HOST} old-domain\.com\folderX [NC]
RewriteRule (.*) http:/new-domain.com/folderX/$1 [R=301,L]

I cannot put a blanket 301 for the old-domain because I still use old-domain.com/folderY, /folderZ etc.

Thank you very much in advance.
Priyank

g1smd

12:35 am on Dec 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rule 2 couldn't possibly work as HTTP_HOST can match only the host name and no path or file name information.

Rule 1 looks like it might be close to what you need, but it redirects only when there is no query string in the original request and it is coded to strip query strings (which aren't there) in the redirect.

The pattern ^folderX$ means EXACTLY example.com/folderX with no more parts in the path. I guess the $ end anchor is an error.

You'll need a combination of bits of Rules 1 and 2, placed in the root .htaccess file.

lucy24

1:23 am on Dec 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rule 2 as typed couldn't work anyway because it contains a slightly blatant typo ;)

By "same location" do you mean that they live side by side in the same directory (userspace, I guess, since you're talking about htaccess) so all requests pass through the same htaccess on the way to their final destination? I'm talking of course about their physical location, not the domain names.

What happens when you try Rule #1?

What's the significance of the QUERY_STRING condition? The ? in the target is superfluous, since you've already constrained the rule to requests that have no query.

If you're on shared hosting, rules specifying /$ for directories may not work. I'm speaking here from direct personal experience: the host may add "index.html" or similar before the request ever reaches your htaccess.

finaltransit

8:33 pm on Dec 28, 2011 (gmt 0)

10+ Year Member



Thanks for the responses g1smd and lucy24,

I admit I am not very familiar with apache syntax so I am reading different forums and trying different ways of doing the 301. For now I have settled on something like this, a combination as g1smd said:

RewriteCond %{HTTP_HOST} ^(.*)old-domain\.com/folderX/.*[NC]
RewriteRule ^(.*)$ http:/new-domain.com/folderX/ [R=301, NC]

I thought adding .* would help but this rule does nothing.

lucy24: You are right, the query string is not useful here.
Re same location: I used to have 3 blogs on old-domain.com

httpdocs
-/folderX
-/folderY
-/folderZ

After I purchased new-domain.com, I did not move any files or create new directories, but merely changed wp config variables for folderX blog so that all paths are now new-domain.com/folderX instead of old-domain.com/folderX. I thought this was easier way to "migrate" folderX blog to the new domain. Both domains are pointing to the same httpdocs directory... did I explain this properly?

And that's why I'm trying to send links pointing to old blog (old-domain.com/folderX) to the new-domain.com/folderX

what else can I try?
Thanks again!

g1smd

9:18 pm on Dec 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need two rulesets.

Follow the instructions above to generate them.

Duplicate, and change one of the targets to https.

Add a RewriteCond to each, one testing for 443 and the other for !443 (not 443).

Remove all the .* bits and don't try testing a path against HOST_NAME. This server variable matches ONLY the host name.