.htaccess mod_rewrite redirection between domains

I have two domains, cshen.ca and cshen.net, both pointing to the same place.

I am trying to do the following with mod_rewrite:

  • cshen.net redirects 301 to cshen.ca
  • www.cshen.net or www.cshen.ca are both redirected to cshen.ca
  • the path after saving the domain after the redirection, for example www.cshen.net/foo/bar/ will be redirected to cshen.ca/foo/bar/

I tried various rules, but can't make it work.

RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://cshen.ca/$1 [R=301,L]

This fulfills the first two rules, but redirects everything back to the main page and does not save the rest of the URL.

RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://cshen.ca/$1 [R=301,L]

'/' RewriteRule URL-, www.cshen.ca. cshen.net, www.cshen.net .

Apache guide :

RewriteCond %{HTTP_HOST} !^cshen\.ca [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cshen.ca/$1 [L,R]

, , URL-, cshen.ca, , , cshen.net www.cshen.net.

, . , - !

:, , URL- WordPress, :

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

WordPress ( , , , RewriteEngine On ), .

!

+3
1

, apache WP

.

0

Source: https://habr.com/ru/post/1713015/


All Articles