简体   繁体   中英

Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS

I have a scenario for redirection I'm having trouble wrapping my head around.

The goals are:

  • old-domain.com/store needs to redirect to new-domain.com/store
  • There are three validation scripts that should remain on Old-Domain.com
  • Everything else from old-domain.com to redirect to new-domain.com
  • These rules need to apply to all scenarios HTTP, HTTPS and WWW (HTTP and HTTPS)

The problem I'm having is that the old-domain.com/store only properly redirects to new-domain.com/store when I remove the WWW redirects. I can't seem to get it to all play together nicely.

Here's what I've got:

Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ http://old-domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ https://old-domain.com/$1 [R=301]
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
Redirect 301 /store/ https://new-domain.com/store
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>

We've also tried the following but then the validation scripts stop passing through:

Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://old-domain.com/%1 [R=301,L]

# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://old-domain.com/$1/ [R=301,L]

RewriteRule ^/?store https://new-domain.com/store [R=301,L]

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]

</IfModule>

The following rewrite rule will redirect all (www.)old-domain.com URLs to https://new-domain.com/ except URLs containing validation-script-(1|2|3).html

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-(1|2|3)\.html$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/$1" [R=301,L]

Check this demo https://htaccess.madewithlove.be?share=f0deda97-dff8-569e-bd6f-411b8a779361

Edit: to redirect old-domain.com/store or old-domain.com/store/ to new- domain.com/pages/store

RewriteRule ^store/?$ "https\:\/\/new-domain\.com\/pages\/store" [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM