Forum Moderators: phranque
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]
If the domains are not similar in that way, then you'll need one-off rules as you've shown.
Jim
[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.
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]
Jim
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?
RewriteCond %{HTTP_HOST} ^([^.]+\.)?exampleABC\.com
RewriteRule (.*) http://www.example.com/ABC/$1 [R=301,L]
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]
Jim
[edited by: jdMorgan at 1:17 am (utc) on April 3, 2008]
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]