1

I'm trying to do a redirect using .htaccess and am running into a problem I can't identify.

What I want to do: Redirect all pages from my old domain to my new domain. For example: https://www.olddomain.com/fun-blog-post/ would forward to https://www.newdomain.com/blog/fun-blog-post/

My code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com/$1 [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com/$1
RewriteRule (.*)$ https://www.newdomain.com/blog/$1 [L,R=301]

Unexpected Behavior: When I type in olddomain.com, it takes me directly to newdomain.com. However, if I type in olddomain.com/new-blog-post, it doesn't redirect at all. It pulls up a 404 error and just displays olddomain.com/new-blog-post.

I've checked the following two threads already.

.htaccess redirect all pages to new domain

Redirect from old domain to new domain not work

However, I cannot figure this one out. I've tried a few different tweaks, most of which I don't remember, but this seems to be the best solution I can come up with. Can someone please help me see what I am doing wrong?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Jeff
  • 13
  • 2

1 Answers1

0
RewriteCond %{HTTP_HOST} ^olddomain.com/$1 [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com/$1
RewriteRule (.*)$ https://www.newdomain.com/blog/$1 [L,R=301]

As written, this wouldn't redirect anything because of the erroneous /$1 at the end of the CondPattern on the RewriteCond directives.

It should be written like this (the two RewriteCond directives can be combined into one):

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://www.newdomain.com/blog/$1 [R=301,L]

This needs to go near the top of the .htaccess file, before any existing directives.

NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.

You will need to clear your browser cache before testing.

When I type in olddomain.com, it takes me directly to newdomain.com

This may be due to a cached redirect, or "something else". The rule you posted would not even trigger this redirect.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • I've got it updated exactly as shown and the behavior isn't changing at all. I've bumped it up to be the first rule without change. I've cleared my cache and even used a different device on a cellular network. So it looks like something else may be interferring with it. At least now I know I've got code in there that SHOULD work once I find what is causing it not to work. – Jeff Oct 11 '21 at 15:19
  • Also, the $1 was a very late add-on in a effort to find SOME way to get the redirect to work or do something different. – Jeff Oct 11 '21 at 15:23
  • @Jeff How are `olddomain.com` and `newdomain.com` configured/setup? They presumably both point to the same place? "When I type in olddomain.com, it takes me directly to newdomain.com" - that is presumably without the `/blog` directory on the target URL? Certainly, if that's the case then "something else" is doing that. Check the network traffic in the browser, do you see a 3xx response somewhere? – MrWhite Oct 11 '21 at 15:28
  • When I type in oldomain.com it is going to newdomain.com/blog. And this is the only forward that I've got in place regarding olddomain. So I'm not sure what is going on. – Jeff Oct 11 '21 at 20:10
  • If you remove (or comment out) this rule, does the redirect stop? Do you have any other directives in your `.htaccess` file? Any other `.htaccess` files in subdirectories? – MrWhite Oct 11 '21 at 20:34
  • When I comment out those lines, it only forwards the olddomain to newdomain, without the /blog. I'm using Nexcess as our host, and have olddomain set as an alias for the new domain. I'm going to have to check with them and see if they have something else on their end that could be working against me. Thank you greatly for your help! – Jeff Oct 13 '21 at 12:33
  • “olddomain set as an alias” - make sure this is not also set as a “redirect” in the hosting control panel. (On cPanel, this is an additional option.) – MrWhite Oct 13 '21 at 12:39
  • I got my hosting provider to step in and take a look at it. It looks like something within iThemes Security or WordPress is messing with the redirect. I have to manually insert it BEFORE their stuff in order to get it to work. Thank you greatly! That's got me up and running! – Jeff Oct 13 '21 at 19:02