3

I have a site that has two domains pointing to it, let's call them:

work.mysite.com
play.mysite.com

This is bad practice, so I want to choose work.mysite.com and make it the canonical URL, permanently redirecting play.mysite.com to it.

I'm in the root directory for these two domains, in a .htaccess file, banging my head against the cement floor and wishing I wasn't here. Here's what I am currently trying. Tell me how totally wrong I am, please?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?play\.mysite\.com [NC]
RewriteRule ^/?(.*)?$ http://work.mysite.com/$1 [R=301]
</IfModule>

That gets me a really pretty 500 Internal Server Error. How far off am I?

rhodesjason
  • 4,904
  • 9
  • 43
  • 59

3 Answers3

6
RewriteCond %{HTTP_HOST} !work.example.com [NC]
RewriteRule ^(.*)$ http://work.example.com/$1 [R=301,L,QSA]

This will also remove the www from www.work.example.com

Not sure if the QSA is needed, but I think it will prevent play.example.com/?home from being redirecting to work.example.com/ instead of work.example.com/?home

Gerben
  • 16,747
  • 6
  • 37
  • 56
  • No need for `QSA` -- its only required if you modify query string, otherwise it will be passed unchanged. – LazyOne Nov 01 '11 at 20:56
  • Use a slash in b/w ^(.*) and $ in second line. Otherwise the new url will ends with // . For instance work.example.com// ... Modified: RewriteCond %{HTTP_HOST} !work.example.com [NC] RewriteRule ^(.*)/$ http://work.example.com/$1 [R=301,L,QSA] – Rameez Oct 04 '16 at 06:51
  • @Rameez in the `.htaccess` context the leading slash is removed. If you use the above code in the `server config` you need to add a slash, but you'd have to add it at the start, not at the end, like you did. So `RewriteRule ^/(.*)$ ...`. Otherwise e.g. `example.com/index.html` will not match! – Gerben Oct 04 '16 at 08:01
1

Ah, I think I was just trying to be too fancy. This seems to work fine:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play.mysite.com$ 
RewriteRule ^(.*)$ http://work.mysite.com/$1 [R=301,L] 
RewriteCond %{HTTP_HOST} ^www.play.mysite.com$ 
RewriteRule ^(.*)$ http://work.mysite.com/$1 [R=301,L]
</IfModule>
rhodesjason
  • 4,904
  • 9
  • 43
  • 59
0

This worked for me.

RewriteCond %{HTTP_HOST} ^jquery\.webcodehelpers\.com
RewriteRule (.*)$ http://uieiq.webcodehelpers.com/$1 [R=301,L]

I used this to redirect a subdomain to another subdomain.

Trevor Meier
  • 81
  • 1
  • 3