[SOLVED] Exclude subdirectory from WordPress redirection

Issue

I’d like to redirect all visitors to my WordPress website except the translations available for pages in the /cy/ subdirectory:

  • /cy/

so for example

  • https://myoldomain.co.uk/cy/partners/

is not redirected but

  • https://myoldomain.co.uk/partners/
  • https://myoldomain.co.uk/

are redirected.

I have tried lots and this one works in an online tester but not in practice – all domains including those with /cy/ are redirected.

RewriteBase /
RewriteCond %{HTTP_HOST} myoldomain\.co.uk
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain\.co.uk/$1 [R=301,L]

Any ideas what I am missing?

Solution

RewriteCond %{HTTP_HOST} myoldomain\.co.uk
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain\.co.uk/$1 [R=301,L]

If you are using WordPress then you have a front-controller pattern later in the file that rewrites all non-files to index.php (the WP front-controller). During the second pass of the rewrite engine /index.php is obvsiously not /cy... so the redirect occurs. But this should be visible in the redirect.

You need to make sure the above redirect only applies to the initial request from the client and not the rewritten request. You can do this by checking against the REDIRECT_STATUS environment variable (which is empty on the initial request).

For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} (^|\.)myoldomain\.co\.uk [NC]
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain.co.uk/$1 [R=301,L]

And this needs to go near the top of the file, before the WP front-controller pattern.

You will need to clear your browser cache before testing. Preferably test with 302 (temporary) redirects to avoid potential caching issues.

Answered By – MrWhite

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *

[SOLVED] Exclude subdirectory from WordPress redirection

Issue

I’d like to redirect all visitors to my WordPress website except the translations available for pages in the /cy/ subdirectory:

  • /cy/

so for example

  • https://myoldomain.co.uk/cy/partners/

is not redirected but

  • https://myoldomain.co.uk/partners/
  • https://myoldomain.co.uk/

are redirected.

I have tried lots and this one works in an online tester but not in practice – all domains including those with /cy/ are redirected.

RewriteBase /
RewriteCond %{HTTP_HOST} myoldomain\.co.uk
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain\.co.uk/$1 [R=301,L]

Any ideas what I am missing?

Solution

RewriteCond %{HTTP_HOST} myoldomain\.co.uk
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain\.co.uk/$1 [R=301,L]

If you are using WordPress then you have a front-controller pattern later in the file that rewrites all non-files to index.php (the WP front-controller). During the second pass of the rewrite engine /index.php is obvsiously not /cy... so the redirect occurs. But this should be visible in the redirect.

You need to make sure the above redirect only applies to the initial request from the client and not the rewritten request. You can do this by checking against the REDIRECT_STATUS environment variable (which is empty on the initial request).

For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} (^|\.)myoldomain\.co\.uk [NC]
RewriteCond %{REQUEST_URI} !^/(cy|cy/.*)$ 
RewriteRule ^(.*)$ https://mynewdomain.co.uk/$1 [R=301,L]

And this needs to go near the top of the file, before the WP front-controller pattern.

You will need to clear your browser cache before testing. Preferably test with 302 (temporary) redirects to avoid potential caching issues.

Answered By – MrWhite

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *