Reputation: 49
I need a help on a site redirection, i have 2 domains : mywebsite.com and mywebsite.org, they both point to the same site (wordpress), and the .com is already indexed by google, i want to redirect all *.com links to www.mysite.org links also all non www of .org to www.mywebsite.org
I have done like this but nothing works :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What i have done wrong here? thank you
Upvotes: 0
Views: 272
Reputation: 45914
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC] RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
As noted in comments, HTTPS_HOST
does not exist, it should be HTTP_HOST
. So, the above condition will simply fail to match and no redirect occurs.
So, if you correct the above variable then this should work to redirect all traffic from mywebsite.com
to www.mywebsite.org
, maintaining the URL-path.
However, since this is WordPress, you should avoid manually editing the directives that are part of the front-controller, ie. the code between the # BEGIN WordPress
and # END WordPress
comment markers. Any redirect that you add should go before the # BEGIN WordPress
block at the top of the `.htaccess file.
This also doesn't redirect the non-www version of the .org
domain.
If you only have these 2 domains then you can simplify the redirect by checking that the requested hostname is not the canonical hostname (ie. is not www.mywebsite.org
) and redirecting accordingly.
Try the following instead:
Options +FollowSymlinks
# Canonical redirect
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.org$
RewriteRule (.*) http://www.mywebsite.org/$1 [R=302,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
<IfModule>
# END WordPress
The !
prefix on the CondPattern negates the regex, so the condition is only successful when it does not match.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure this is working OK - to avoid potential caching issues.
Make sure you have cleared your browser cache before testing.
Note that you are redirecting to HTTP (not HTTPS) - is that intentional?
Upvotes: 1