redirect - Redirections and rewrites to subdomain

I am currently working on a new WordPress website, to partially replace a non-WP website that includes a webshop. Now, t

I am currently working on a new WordPress website, to partially replace a non-WP website that includes a webshop. Now, the plan is to migrate the webshop and all matching pages (shopping card, order confirmation page, etc.) to a subdomain, while keeping the informative pages (like About Us and Contact) on the main directory. Those informative pages on the main directory will be replaced by WordPress pages with the exact same urls (one page is the exception).

Thankfully, I'm not in charge of the migration, but I have been tasked with writing the correct redirections... and that's where I get stuck.

In short, the whole new website would be set up like this:

  • /
  • /
  • /
  • /
  • /

What I've got so far is this:

# Redirect this one page to a new url
Redirect 301 /privacy-statement /

# Make sure these pages and their children aren't redirected
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/about-us/(.*)
RewriteCond %{REQUEST_URI}!^/contact/

# Redirect the rest to the new subdomain
RewriteRule (.*) /$1 [R=301,L]

I am however not certain if this is correct... Can anyone confirm if it is correct, or give a hint regarding what I should change?

I am currently working on a new WordPress website, to partially replace a non-WP website that includes a webshop. Now, the plan is to migrate the webshop and all matching pages (shopping card, order confirmation page, etc.) to a subdomain, while keeping the informative pages (like About Us and Contact) on the main directory. Those informative pages on the main directory will be replaced by WordPress pages with the exact same urls (one page is the exception).

Thankfully, I'm not in charge of the migration, but I have been tasked with writing the correct redirections... and that's where I get stuck.

In short, the whole new website would be set up like this:

  • https://example/
  • https://example/about-us/
  • https://example/contact/
  • https://example/privacy/
  • https://shop.example/

What I've got so far is this:

# Redirect this one page to a new url
Redirect 301 /privacy-statement https://example/privacy/

# Make sure these pages and their children aren't redirected
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/about-us/(.*)
RewriteCond %{REQUEST_URI}!^/contact/

# Redirect the rest to the new subdomain
RewriteRule (.*) https://shop.example/$1 [R=301,L]

I am however not certain if this is correct... Can anyone confirm if it is correct, or give a hint regarding what I should change?

Share Improve this question edited May 26, 2019 at 6:59 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked May 25, 2019 at 14:23 YvonneYvonne 254 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0
Redirect 301 /privacy-statement https://example/privacy/

You should avoid using a mod_alias Redirect directive when you are already using mod_rewrite for other redirects. mod_alias runs after mod_rewrite, regardless of the apparent order of the directives in your .htaccess file, so it's possible to get unexpected conflicts. This should be converted to a mod_rewrite (RewriteRule) redirect instead:

RewriteRule ^privacy-statement$ /privacy/ [R=302,L]

Note that this redirects the specific URL /privacy-statement, nothing more (which is what I assume you require). The Redirect directive is prefix-matching so it potentially matches a lot more (but you have a slash mismatch on the source vs target URL, so this could have potentially broken something).

I assume you are redirecting from/to the same domain here, so I've not included the domain in the substitution.

Note also, that I've used a 302 (temporary) redirect here. It is a good idea to test with 302s, before changing to 301 (permanent) - if that is the intention - only once you have confirmed that everything works OK. 301s are cached persistently by the browser so can make testing problematic.

# Make sure these pages and their children aren't redirected
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/about-us/(.*)
RewriteCond %{REQUEST_URI}!^/contact/

# Redirect the rest to the new subdomain
RewriteRule (.*) https://shop.example/$1 [R=301,L]

This looks mostly OK, however, you are missing spaces between the TestString and the CondPattern in both the RewriteCond directives - so this will fail.

The (.*) on the end of the CondPattern is superflous. Both regex match /about-us/<anything> and /contact/<anything> (or rather, "don't match", since the regex is negated).

You only need the RewriteEngine directive once in the file. It can actually go anywhere, even at the very end, although it is logical to include it at the top, before any mod_rewrite directives. (The WordPress front-controller - which you should not edit - already contains the RewriteEngine On directive, so you don't need to repeat it. But there is no harm if you do.)

And these redirects need to go before any WordPress front-controller that might follow.

In summary:

RewriteEngine on

# Redirect this one page to a new url
RewriteRule ^privacy-statement$ /privacy/ [R=302,L]

# Make sure these pages and their children aren't redirected
RewriteCond %{REQUEST_URI} !^/about-us/
RewriteCond %{REQUEST_URI} !^/contact/

# Redirect the rest to the new subdomain
RewriteRule (.*) https://shop.example/$1 [R=302,L]

Again, test with 302s.

I'm assuming that your main domain and the subdomain point to different areas on your filesystem (or even different filesystems), otherwise you'll need an additional condition that checks the requested Host before redirecting.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745463589a4628817.html

相关推荐

  • redirect - Redirections and rewrites to subdomain

    I am currently working on a new WordPress website, to partially replace a non-WP website that includes a webshop. Now, t

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信