3

Using .htaccess I am trying to setup a simple 301 redirect which redirects both exampleA.com and www.exampleA.com (the old domain) to exampleB.com (the new domain). However, accessing www.exampleA.com results in a 403 Forbidden status.

Both exampleA.com and exampleB.com point to the same hosting account.

So, in summary:

  • http://exampleA.com redirect to http://exampleB.com - OK
  • http://www.exampleA.com redirect to http://exampleB.com - 403 Forbidden

.htaccess code:

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^exampleA\.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.exampleA\.com$
  RewriteRule ^(.*)$ http://exampleB.com/$1 [L,R=301]]
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
  RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://exampleB.com/$1 [L,R=301]
</IfModule>

Note: I am making these changes in the htaccess file of the current new domain website.

  1. How do I need to change the syntax to redirect both non-www and www to the new domain without getting a 403 response?
  2. If I make changes to the htaccess file are these reflected instantly or do they take time?
  • 1
    I am making these changes in the htaccess file of the current new domain website. Err? You can't do that unless they share the same file space, which isn't a good idea. You should use two .htaccess files, one for the old domain and where that is hosted, i.e /var/www/public_html/exampleA.com and then another in /var/www/public_html/exampleB.com. Your exampleA (old domain should redirect everything to the new domain, no need to setup rules in the .htaccess for the new domain. – Simon Hayter Feb 10 '16 at 15:14
  • Thanks Simon - Thanks Simon - in reply. I changed the domain by mapping the old domain to the new domain but not And kept the original web site. Then I changed all the old domain URLs to the new domain. But am only struggling with the redirect of the naked domain - whatever I have done redirects fine the www.olddomain.co.uk but not the olddomain.co.uk – Red Wolf Feb 11 '16 at 0:59
  • If you have changed the site file paths to the same web root, then you do not need to do a redirect. This is a good solution. But for those who are confused on these issues, I recommend against it. Before your change, your redirects from site A to site B would have to been made within the site A .htaccess file and not site B. – closetnoc Feb 11 '16 at 5:35
  • @closetnoc You'll need to do a redirect if need to move the user from exampleA.com to exampleB.com? – MrWhite Feb 11 '16 at 9:25
  • 1
    @w3dk My point is that redirecting from site A to site B, the code cannot be on site B, it must be on site A or it will not be seen. Perhaps I did not write my comment well. Sorry. Cheers! You got the OP; I know they are in the best hands possible. – closetnoc Feb 11 '16 at 15:39
1

As mentioned in comments, what you already have is "OK" - it should work in terms of redirecting exampleA.com (both www and the naked domain) to exampleB.com. So I suspect the problem is somewhere else. However, what you have could be tidied and improved. So, I don't think this answer is really a solution, but a slightly different way of doing the same thing - which might help.

Since both the old and new domains point to the same place, you don't need to specifically check for the old domain. You can alternatively write... if it's not the new domain then redirect to the new domain. This will also handle your canonical www to exampleB.com redirect, which you have not currently implemented.

So, instead of:

RewriteCond %{HTTP_HOST} ^exampleA\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.exampleA\.com$
RewriteRule ^(.*)$ http://exampleB.com/$1 [L,R=301]]

(Incidentally, you have an extra ] at the end of the RewriteRule - although this won't result in an error, it will simply be ignored.)

Can be rewritten as:

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

If the request is not exampleB.com then redirect to exampleB.com. Note the NC flag on the RewriteCond directive. This is not for normal users (browsers always lowercase the host), but for bots/scripts that might make malformed requests.

  1. If I make changes to the htaccess file are these reflected instantly or do they take time?

On a simple client/server setup they should be reflected instantly.

However, be careful of (browser) caching. 301 redirects are cached by the browser (and perhaps intermediary proxies). This includes any 301 "mistakes" you've previously requested. In Google Chrome you can run with the Object Inspector open and caching disabled. Alternatively, test with 302 (temporary) redirects (which are not cached) and only change to a 301 when you are sure it's working OK.

# BEGIN WordPress

You have a comment that suggests WordPress, however, the directives don't look very WordPress to me?

Also, if you have other directives in your .htaccess file or server config they could be conflicting and causing this error.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.