views:

36

answers:

1

I have two domains in the format of foo.com and foo.net. I currently have the following in my .htaccess to redirect non-www to www:

# Rewrite URL to force WWW
  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

However, I want to redirect URLs ending in .net to go to .com, including the subdomains. Examples:

foo.net -> foo.com
www.foo.net -> www.foo.com
foo.net/bar -> foo.com/bar
sub.foo.net -> sub.foo.com
sub.foo.net/bar -> sub.foo.com/bar

I used to have this on another site, but it was years ago and I don't have the .htaccess anymore. Does anybody know an easy way of doing this?

Edit: Here is the rest of the .htaccess:

# BEGIN WordPress
#
  RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# if this request is for "/" or has already been rewritten to WP
  RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
  RewriteCond $1 \.(gif|jpg|png|php|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
  RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
  RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
  RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
  RewriteRule . /index.php [L]
#
# END wordpress 

# Rewrite URL to force WWW
   -(see top of post)-
+1  A: 

Ignoring the fact that this is by far the most devilish rewrite ruleset that I've ever written, this surprisingly seems to take care of what you currently have, plus what you want, in a nice, compact package.

Let me know if you have any problems with it:

RewriteCond %{HTTPS} =on
RewriteRule .* - [E=RW_HTTPS:s]

RewriteCond %{HTTP_HOST}     ^www\.(.*\.[^.]*\.[^.]*)$ [NC,OR]
RewriteCond www.%{HTTP_HOST} ^(www\.[^.]*\.[^.]*)$
RewriteRule .* - [E=RW_THOST:%1]

RewriteCond %{ENV:RW_THOST}  ^(.*)\.(net|com)$         [NC,OR]
RewriteCond %{HTTP_HOST}     ^(.*)\.net$               [NC]
RewriteRule .* http%{ENV:RW_HTTPS}://%1.com%{REQUEST_URI} [R=301,L]
Tim Stone
It didn't work when I replaced my current ruleset with yours (internal 500 error) but placed after my current set has a problem - whenever it rewrites a matching url it cuts off the rest of the URL (%{REQUEST_URI}), leaving you with www.foo.com
StormPooper
Do you have access to your server's access and error logs? It works fine on my test server (even with the WordPress rules), so I'm at a loss why it would it would cause a 500 error in your environment. If we can't figure that out I can write up a less compact solution that you can use instead.
Tim Stone