Forum Moderators: phranque

Message Too Old, No Replies

301 redirect secondary domain to folder on primary domain

This is my first exposure to .htaccess

         

Huligan

5:04 am on Apr 2, 2008 (gmt 0)

10+ Year Member



First, let me apologize if this is too elementary. It seems easy to me but I have not been able to find an answer. I have been browsing and searching this forum (on & off) for 2 days, but can't find what I'm looking for. I may not have been searching with the correct phrase so feel free to provide a more accurate search phrase. I have seen more complicated problems that are similar to mine, but can't simplify the code to work for me.

I need a few 301 redirects. The first one I need is this.

http://example.com > http://www.example.com

I got this one to work and this is my primary domain. Here's the part where I can't find a solution. I have several domains that are the full names of different widgets. I want to 301 redirect these secondary domains to a folder (specific to each widget) on the primary domain. Here are some examples.

http://www.exampleXYZ.com > http://www.example.com/XYZ
http://www.example123.com > http://www.example.com/123
http://www.exampleABC.com > http://www.example.com/ABC

I also need the non-www version to point to the same place.

http://exampleXYZ.com > http://www.example.com/XYZ
http://example123.com > http://www.example.com/123
http://exampleABC.com > http://www.example.com/ABC

I have seen enough posts on this forum to see this is a "self-help" forum. Here's the current version of my .htaccess.

RewriteEngine on

#301 Redirect example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com [R=301,L]

#301 Redirect widgetXYZ.com to example.com/XYZ
RewriteCond %{HTTP_HOST} ([^.]+).exampleXYZ.com
RewriteRule ^(.*)$ http://www.example.com/XYZ [R=301,L]

#301 Redirect widget123.com to example.com/123
RewriteCond %{HTTP_HOST} ([^.]+).example123.com
RewriteRule ^(.*)$ http://www.example.com/123 [R=301,L]

#301 Redirect widgetABC.com to example.com/ABC
RewriteCond %{HTTP_HOST} ([^.]+).exampleABC.com
RewriteRule ^(.*)$ http://www.example.com/ABC [R=301,L]

I have about 10 domains I want to do this for so I'm also interested in an optimized solution. Thank you for your help.

[edited by: jdMorgan at 2:25 pm (utc) on April 2, 2008]
[edit reason] example.com [/edit]

jdMorgan

2:18 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer to your question depends on how literally we can take the "exampleABC" and "example123" format. If all domains literally start with the same "example" domain-name part, then your problem can be solved with only two rules.

If the domains are not similar in that way, then you'll need one-off rules as you've shown.

Jim

Huligan

3:00 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



There is not a literal translation from domain to folder name. For example, there will be variations like the following.

[exampleXYZ.com...] > http://www.example.com/XYZ-kit
[example-123.com...] > http://www.example.com/123-sku
[ABC-example.com...] > http://www.example.com/AaBbCc

Some domains refer to how the customer references the product, but the website references the product the way the company does. This is why there will be some variances. I figure this will mean each domain will get its own rule.

jdMorgan

8:44 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sounds like you'll need one-off rules, then.

Note that literal periods in regular-expressions patterns must be escaped. Otherwise they are taken as regex tokens meaning "any single character."


RewriteCond %{HTTP_HOST} ([^.]+[b])\.e[/b]xampleAB[b]C\.c[/b]om
RewriteRule (.*) http://www.example.com/ABC [R=301,L]

As shown, periods within character alternative [groups] need not be escaped; They are taken as literals by default.

Jim

Huligan

12:44 am on Apr 3, 2008 (gmt 0)

10+ Year Member



This solution worked partially for me.

RewriteCond %{HTTP_HOST} ([^.]+)\.exampleABC\.com
RewriteRule (.*) http://www.example.com/ABC [R=301,L]

This code did the following.

Worked: [exampleABC.com...] -> http://www.example.com/ABC
Failed: [exampleABC.com...] -> http://www.example.com/ABC

I need both the www and non-www versions of the secondary domain to 301 redirect to the folder on the primary domain. Also tried this code.

RewriteCond %{HTTP_HOST} ^exampleABC\.com
RewriteRule (.*) http://www.example.com/ABC [R=301,L]

This is what I got with this code.

Failed: [exampleABC.com...] -> http://www.example.com/ABC
Worked: [exampleABC.com...] -> http://www.example.com/ABC

In order to get both the www and non-www versions to redirect to the same place, do I need both sets of code or is there a more consolidated code that will do both?

jdMorgan

1:15 am on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just make the subdomain optional by using a "?" on the parenthesized subpattern:

RewriteCond %{HTTP_HOST} ^([^.]+\.)?exampleABC\.com
RewriteRule (.*) http://www.example.com/ABC/$1 [R=301,L]

I suppose at this point I should ask if you really want to remove the old domains from search engine listings, and replace them with www.example.com/<domain>/

If not, you need to use an internal rewrite, and not an external redirect:


RewriteCond $1 !^ABC/
RewriteCond %{HTTP_HOST} ^([^.]+\.)?exampleABC\.com
RewriteRule (.*) /ABC/$1 [L]

The additional RewriteCond prevents an 'infinite' internal rewriting loop.

Jim

[edited by: jdMorgan at 1:17 am (utc) on April 3, 2008]

Huligan

1:47 am on Apr 3, 2008 (gmt 0)

10+ Year Member



I want people to be able to use the secondary domains (exampleABC.com) for a long time, but I want the search engines to know the primary domain and folder (examples.com/ABC). This is why I'm using a 301 redirect. The primary domain should always be what the search engines link to.

With this said, what code should I use? Will the first example do the trick? The 301 redirect is the main requirement in all of this since users will be creating links with these secondary domains.

RewriteCond %{HTTP_HOST} ^([^.]+\.)?exampleABC\.com
RewriteRule (.*) http://www.example.com/ABC/$1 [R=301,L]