Rehmat
Rehmat

Reputation: 5071

A specific 301 redirection to another domain via htaccess

There are a lot of answers here at Stack Overflow regarding the redirection via htaccess and I have some basic understanding about this. Instead of redirecting the same paths to the new domain, I need these results:

  1. Redirect both olddomain.com and www.olddomain.com to hxxps://www.newdomain.com
  2. Redirect www.olddomain.com/example-page to hxxps://www.newdomain.com/somepage

Please note in condition 2 that I want to redirect the subpages to different paths on new domain. The first part is easy for me but the second one challenges me. How can I achieve these results (for 301 redirect)?

Upvotes: 1

Views: 58

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

Try:

RewriteEngine On

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

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

Alternatively, if you are using mod_alias to do the redirecting, then your second redirect needs to be a RedirectMatch and not the Redirect directive:

RedirectMatch 301 ^/example-page https://www.newdomain.com/somepage

Upvotes: 1

Related Questions