Mobile app version of vmapp.org
Login or Join
Sent6035632

: Trouble with 301 redirect of old naked domain to new domain Using .htaccess I am trying to setup a simple 301 redirect which redirects both exampleA.com and www.exampleA.com (the old domain)

@Sent6035632

Posted in: #301Redirect #Apache #Htaccess

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

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

So, in summary:

exampleA.com redirect to exampleB.com - OK www.exampleA.com redirect to 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 ^(.*)$ exampleB.com/ [L,R=301]]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*(default|index).(html|php|htm) HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index).(html|php|htm)$ exampleB.com/ [L,R=301]
</IfModule>


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


How do I need to change the syntax to redirect both non-www and www to the new domain without getting a 403 response?
If I make changes to the htaccess file are these reflected instantly or do they take time?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Sent6035632

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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 ^(.*)$ exampleB.com/ [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 (.*) exampleB.com/ [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.



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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme