homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirecting subfolder pages to new domain
Leaving homepage on one domain and re-directing other pages

 2:26 am on Feb 20, 2013 (gmt 0)


I am moving a website from a self-managed platform to a hosted solution. It's an e-commerce website.

The idea is to leave a homepage on the self-managed server with links pointing to the new site, which has a sub-domain attached to it.

Domain is www.example.com and the sub-domain is store.example.com.

I have created a rule to do this and it is working fine.

Also, whilst in transition I don't want to disable the old store, I want to re-direct the individual links to products and store categories based on the requested URL. This is because if someone is searching for a particular product, rather than visiting the store front, the results from search engines are for the old store links.

So, I want to be able to redirect any requests for www.example.com/store/product.html to store.example.com/product.html.

Here are the contents of the .htaccess I have been working on.

RewriteEngine on

DirectoryIndex index.html
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^store/(.*)$ "http\:\/\/store\.example\.com\/$1" [R=301]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^example.com "http\:\/\/example\.com\/index\.html" [R=301,L]

The second rule works put not the first.

Looking for help to get it working please.

And, if anyone can provide some background info on the impact on SEO of doing this, that would be gratefully received.

Thanks in advance.



 7:47 am on Feb 20, 2013 (gmt 0)

Does "example" represent the same word for the new and for the old site?

The code can be much simplified.

Do not escape anything in the target URL of both redirects. Remove the quotes too.

Never redirect to a named index page. The canonical URL for the root page is
http://www.example.com/ with a trailing slash.

I'm not sure what you're trying to do witb the second rule.
RewriteRule ^example.com http://example.com/ [R=301,L]
redirects a request for
http://example.com/exampleXcom<anything> or for
where X is any character and
<anything> is "anything" or "nothing".

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$

simplifies to
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
or might not be required at all.


 11:48 am on Feb 20, 2013 (gmt 0)

Thanks for the feedback g1smd.

Firstly, yes the domain example.com is the same for both sites. The new site is a new sub-domain pointing to the hosted server.

I added the second rule because I was having difficulty changing an existing redirection from the www root page to the root of the store folder.

I have removed that second rule now and it is working fine.

The redirect of the sub-folder is still an issue for me though.

The single rule in .htaccess is now
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\/store\/(.*)
RewriteRule http\:\/\/store\.example\.com\/ [R=301,L]

I want to redirect any traffic that is looking for the old path to the new path, where
old path = http://www.example.com/shop/product.html
new path = [store.example.com...]

Can this be achieved using the (.*) option? Should I be editing the .htaccess in the old subfolder http://www.example.com/shop/.htaccess?

Appreciate your help in working through this.


 12:04 pm on Feb 20, 2013 (gmt 0)

(.*) is not an "option". It's a regular expression looking at the path part of the URL request. It means "match any character, zero or more times. To match more "specific" requests, you replace with .* with a more specific pattern.

The last example you gave can never work. The syntax is completely broken.

HTTP_HOST can only match hostname and no other part of the URL request. The RegEx pattern for the rule is completely missing. Move the
\/store\/(.*) bit from the Condition to the Rule and change it to ^shop/(.*) and you'll be on the way.

The target URL must not be escaped. Append /$1 to the end of the target, to re-use the captured information in the redirect.


 12:25 pm on Feb 20, 2013 (gmt 0)

Uh... You've got example.com and store.example.com living in entirely different physical locations? With DNS set up to point requests for store.example.com to the new place and plain example.com to the old place? I'm getting a headache just thinking about it.

What are the Conditions for?
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com

If this is happening in your old site's htaccess then the host can't be anything but your basic domain, so you don't even need to consider it. A simple, conditionless

RewriteRule ^store/(.*) http://store.example.com/$1 [R=301,L]

should do it.

Other than example.com/store/blahblah which is now going to store.example.com/blahblah, what's left at example.com ?


 1:03 pm on Feb 20, 2013 (gmt 0)

One point of confusion.

Is the old site at example.com/shop/ or example.com/store/ ?


 11:59 pm on Feb 20, 2013 (gmt 0)

My input error, the naming convention should be consistent - either store or shop in all uses.

Thanks for your help.

As I mentioned, we are moving a store from a self-managed platform to a hosted platform and want to retain full control over the homepage - not limited to the hosted solution framework. So, there is nothing much left on the old site, except for the history etc. We do now have a way of building a more developed homepage.

Thanks for your help with simplifying the RewriteRule.

It is now working as desired, I established that the simple RewriteRule needs to be in the old store subfolder, not the www root, with the rule as

RewriteRule (.*) http://store.example.com/$1 [R=301,L]

Thanks again.

 12:19 am on Feb 21, 2013 (gmt 0)

You're often better off preceding the (.*) pattern with ^shop/ and moving the rule to the beginning of the root htaccess.


 12:37 am on Feb 21, 2013 (gmt 0)

Didn't seem to work but I will add it back into the root htaccess too.

Thanks for the tip.


 1:18 am on Feb 21, 2013 (gmt 0)

we are moving a store from a self-managed platform to a hosted platform

That was the part I wanted to double check. 99 times out of 100, subdomains will physically live in directories within the directory that contains the primary domain. This means that any requests for the subdomain have to pass through the main domain's config file (and htaccess if any).

But it sounds as if you've got the 100th case, where the two things live in entirely different locations. That means that requests for the subdomain will not pass through the primary domain's config/htaccess, and vice versa. It can make rewrites a lot easier, but it still gives me a headache.

Overlapping g1 here
I established that the simple RewriteRule needs to be in the old store subfolder, not the www root

It can go either way. The main difference is that if the rule is in the (old) root, you need to include "store/" at the beginning of the rule; if it's in the subfolder you don't need it because the request has already been filtered down.

But rules of any kind should be as close to the "top" (config file or root-level htaccess) as possible. Otherwise the server is doing the extra work of sending requests along to the next subdirectory-- when they're going to end up being redirected anyway.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved