23

I have a website with roughly 1K URLs. The website is moving to a different domain name. The URLs will be the exact same though, otherwise. I'd like to incorporate an htaccess or some kind of rule that does a 301 redirect for all URLs in one fell swoop. It would essentially replace the domain name as a 301 redirect.

Example:

  • Current URL: domain.example/blog/post-1.html
  • Redirect To: newdomain.example/blog/post-1.html

And that performed as a 301 redirect. How would I do that?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
hdwebpros
  • 528
  • 3
  • 7
  • 20

3 Answers3

43

Place this redirect rule in your DOCUMENT_ROOT/.htaccess file of domain.example:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.example$ [NC]
RewriteRule ^ http://newdomain.example%{REQUEST_URI} [L,R=301,NE]

Details:

  • Condition RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.example$ matches when host name in request is either www.domain.example or domain.example.
  • RewriteRule redirect all the URLs to newdomain.example with the URI exactly same as in the original request.
  • R=301 sets HTTP status code to 301 (permanent redirect)
  • NE is for no escaping to avoid encoding of special characters (if any) from original requests
  • L is for last rule
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Is this working in 2022? I have tested and It's only redirecting www website. – Naren Verma Mar 08 '22 at 08:21
  • It definitely works. There is nothing in that rule that would stop working after few years. Make sure this rule is your **topmost** rule and you clear your browser cache. – anubhava Mar 08 '22 at 08:22
  • sir, Yes, I have tested, Clear the cache as well. When I hit the URL along with www.test.com then it's redirecting on the new domain but if I hit test.com then it's not redirecting. – Naren Verma Mar 08 '22 at 08:25
  • ok check updated answer. You need to make `www.` part optional – anubhava Mar 08 '22 at 08:26
  • I have tested the @Clinton answer and that is working. – Naren Verma Mar 08 '22 at 08:26
  • 1
    sir, Yes, Now it's working. Thank you for the help. Upvote from my side – Naren Verma Mar 08 '22 at 08:35
  • @anubhava It is not working for me. Can you check what's wrong? I can't share it in the comments so uploading on this link https://ibb.co/z8dFwcm – paul Jun 27 '22 at 14:59
  • Unfortunately `not working for me` has no details. I suggest you post a new question with more details so that I can understand and try to answer. – anubhava Jun 27 '22 at 15:01
  • @anubhava Before I could fix my comment, you commented back :). Please check now the same comment. When I hit my old blog URL it doesn't redirect to the new blog URL `old.`https://yogixi.com/automation-testing/step-by-step-create-a-clean-pull-request-in-git/ `new` https://mintqa.com/automation-testing/step-by-step-create-a-clean-pull-request-in-git/ – paul Jun 27 '22 at 15:03
  • This is very basic and working redirect rule. Just make sure this is your topmost rule in .htaccess and there is no other conflicting rule. – anubhava Jun 27 '22 at 15:11
  • @anubhava I shared the `.htaccess` file here https://ibb.co/z8dFwcm. I have copy-pasted your solution on top, do you see any conflict? – paul Jun 27 '22 at 15:24
  • Your .htaccess shows olddomain, newdomain instead of actual domain names. Anyway since you're using WP here, you should also check `wp-config.php` settings to get domain right – anubhava Jun 27 '22 at 15:26
12

When moving a domain name to a new domain where the only change to the URL is the domain name, I use the following redirect in my Apache .htaccess file

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^domain.example$ [OR]
  RewriteCond %{HTTP_HOST} ^www.domain.example$
  RewriteRule ^(.*)$ http://newdomain.example/$1 [R=301,L]

This ensures that all links in the old site are redirected and search engines like Google, Bing etc. are aware that the domain was permanently moved. This has the benefit that any ranking from domain.example is transferred to newdomain.example.

Note: you will need to change /$1 to $1 if you include RewriteBase / in your .htaccess.

This is an alternative to the method shown above.

Clinton
  • 1,111
  • 1
  • 14
  • 21
  • 1
    Note you can also use `RewriteCond %{HTTP_HOST} =www.domain.com` so this is a normal string comparision rather than a regex match. This is likely faster and less likely that you'll encounter something unexpected (e.g. in this case the meaning of the dot (.) which is not escaped in this answer) – yankee Mar 20 '19 at 17:41
  • 1
    working for me. Thank you. Upvote from my side. – Naren Verma Mar 08 '22 at 08:42
  • The accepted answer was not working for me but this one is only if I change the $1 on the 3rd line for %{REQUEST_URI} – Carol May 24 '23 at 05:54
  • 1
    Thank you for your feedback @Carol. I am not sure why the $1 did not work for you, but using %{REQUEST_URI} is a good workaround. Using %1 should however work for everyone as this is standard RegEx syntax and I have found this to be very reliable. The only thing to watch for if you find the code does not work is whether you need to add a leading '/' to $1 so that the last line becomes: RewriteRule ^(.*)$ http://newdomain.example/$1 [R=301,L] This is needed if you have nor set RewriteBase /. I will change the answer above to include the / as I am sure this is the most usual case. – Clinton May 25 '23 at 09:34
3

With www or without

RewriteEngine On
    
RewriteCond %{HTTP_HOST} (w*)domain\.example$ [NC]
RewriteRule ^ http://newdomain.example%{REQUEST_URI} [L,R=301]
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • `(w*)domain\.example$` wouldn't actually match `www.domain.example` . Did you mean `(w*\.)?domain\.example$`? And why `(w*)` rather than `.*` or `www`? – Stephen Ostermiller Mar 08 '22 at 20:34