I am creating a new website on a new domain, and doing page-to-page redirects from the old website for (A) all static pages, as well as (B) all blog posts.

A) The static pages follow a new URL categorization and naming pattern, eg.

http://olddomain.com/doctors/ becomes https://newdomain.com/about-us/team/

B) The rest are a whole bunch of blog posts that will keep the same URL pattern, they are /postname/, so I just need to map them to the new domain in the root. Eg.

http://olddomain.com/hello-world-blog-post/ becomes https://newdomain.com/hello-world-blog-post/

Here is an example of what I tried (didn't work):

# 1. Redirect to domain without www
# ... This is if people try www.olddomain.com, or there are links out there pointing to this
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]

# 2. 301 Redirect URLs
Redirect 301 /doctors/ https://newdomain.com/about-us/team/
Redirect 301 /us/ https://newdomain.com/about-us/

# 3. Redirect to another domain: newdomain.com
# ... This is to send http://olddomain.com/hello-world-blog-post/ to https://newdomain.com/hello-world-blog-post/

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC]
RewriteRule .* http://newdomain.com%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule .* https://newdomain.com%{REQUEST_URI} [R=301,L]

What Happened?

When I tried testing by visiting http://olddomain.com/doctors/, rather than catching the Redirect 301 rule (Redirect 301 /doctors/ https://newdomain.com/about-us/team/), it simply redirected to the new domain, ie, https://newdomain.com/us/, which of course generated a 404 page not found error on the new domain.

So it seems to be ignoring the 2nd batch of rules and just doing the 3rd.

  • PS - I used beamusup.com/generate-htaccess to generate this, FYI! – Drewdavid May 7 '18 at 20:14
  • 1
    Mixing mod_alias rules (Redirect) with mod_rewrite rules (RewriteRule) can lead to precedence problems. Have you tried using all rewrite rules? – Stephen Ostermiller May 7 '18 at 20:27
  • Are olddomain.com and newdomain.com pointing to the same host? If so then presumably you have more directives in your .htaccess file (ie. some kind of front-controller that routes your blog posts)? Please post your entire .htaccess file. Even accounting for the mix of mod_alias and mod_rewrite directives that @Stephen noted, there is nothing in what you've posted that would perform the redirect you are reporting (ie. http://olddomain.com/doctors/ to https://newdomain.com/us/)? In fact, none of the above actually redirects HTTP to HTTPS at all? – MrWhite May 7 '18 at 22:33
  • How does lighthousehealth.ca fit into your olddomain.com / newdomain.com example? I think you may have exemplified your rule(s) incorrectly in your question? The last rule would result in a redirect loop. Also make sure you clear your browser cache and test with 302 (temporary) redirects to avoid the browser cache giving you additional grief. – MrWhite May 7 '18 at 22:37

Your Answer

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

Browse other questions tagged or ask your own question.