1

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.com/
  • https://example.com/about-us/
  • https://example.com/contact/
  • https://example.com/privacy/
  • https://shop.example.com/

What I've got so far is this:

# Redirect this one page to a new url
Redirect 301 /privacy-statement https://example.com/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.com/$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?

0

1 Answer 1

0
Redirect 301 /privacy-statement https://example.com/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.com/$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.com/$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.

1
  • Thank you so much! I have however chosen to use a redirection plugin instead, and forward all the webshop links to their matching pages on shop.example.com. That way, I won't have to add a new redirect each time I add a new page on the WordPress website. In any case, I am still very thankful for your help and hope the solution may be of use to other people.
    – Yvonne
    Commented May 27, 2019 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.