3

i m using wordpress and want to redirect my root domain to a new subdomain and ignore urls that contains a certain string

http://www.example.com/post.html To

http://subdomain.example.com/post.html

And exclude

http://www.example.com/post-exclude.html

So that i can deploy a new blog while i am redirecting the old one

What i have tried :

RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]

But it's redirecting to the main subdomain.example.com and ignoring the path

Full Code

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
  • The code you posted looks like it should work? When you say it's "ignoring the path", is it ignoring the exclude path or always ignoring the path and redirecting to the root? Where in your .htaccess file have you placed this code? Please show the directives in-place - there might be a conflict with other directives. You should also test with 302 (temporary) redirects to avoid potential caching issues. Make sure you've cleared your browser cache. – MrWhite Jun 15 at 23:59
  • It redirects to subdomain.example.com/ instead of subdomain.example.com/some-post-exclude , while it's working when the path is not containing the exclude – AYMEN SOUMER Jun 16 at 0:09
  • also i kept the wordpress htaccess RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] – AYMEN SOUMER Jun 16 at 0:15
  • Please edit your question to add the relevant code, with the directives you've added in-place. "It redirects to subdomain.example.com/ instead" - that wouldn't seem possible with just the code you posted, so there's likely a conflict if you've put the code in the wrong place for example? – MrWhite Jun 16 at 0:24
  • i have posted the full code – AYMEN SOUMER Jun 16 at 0:30
3
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

The WordPress front-controller conflicts with your redirect and ultimately causes a redirect to /index.php. (Although that is not strictly the same as / - as you've stated - maybe there is something else that removes the index.php part, if it's not present in the resulting URL?)

When you initially request /post-exclude.html, the RewriteCond directive prevents the redirect, but after the request is internally rewritten to /index.php (by the WordPress front-controller below), processing then loops which then triggers the redirect to /index.php (the homepage). The internal rewrite results in the REQUEST_URI server variable being updated to /index.php (whilst the visible URL remains as /post-exclude.html).

In other words, when you request /post-exclude.html:

  1. Redirect code is bypassed because of the RewriteCond (condition) directive.
  2. Request is internally rewritten to /index.php (by the WordPress front-controller)
  3. The rewrite engine starts over... (as it does when the URL is rewritten)
  4. Now the redirect triggers since it is checking against /index.php, not /post-exclude.html (whilst the visible URL remains as /post-exclude.html).
  5. Redirects to http://subdomain.example.com/index.php.

You can avoid this second internal rewrite triggering the redirect by making sure you only target the initial request in the redirect. You can do this by either checking against THE_REQUEST instead (which contains the first line of the HTTP request headers) or the REDIRECT_STATUS environment variable (which is empty on the initial request and set to "200" - as in 200 OK HTTP status - after the first successful rewrite).

You should also avoid manually editing the code between the # BEGIN WordPress and # END WordPress comment markers as this is maintained by WordPress and your manual edits could be overwritten in a future update.

The redirect should go before the # BEGIN WordPress line.

RewriteCond %{REQUEST_URI} !exclude

Note that this condition excludes any URL that simply contains exclude (case-sensitive) anywhere in the requested URL. Whereas the example you posted looks quite specific. Note that if you only want to exclude a single URL, you don't need an additional condition as this can all be done in the RewriteRule directive.

Try the following instead:

# Redirect to subdomain.example.com, excluding some URL(s)
# NB: REDIRECT_STATUS - Check the initial request only (not the rewritten request)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule (.*) http://subdomain.example.com/$1 [L,R=302]

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

As noted in comments, test with 302 (temporary) redirects and make sure your browser cache is cleared.

| improve this answer | |
  • It's not working for me, it's not excluding url that contains "post-exclude" – AYMEN SOUMER Jun 16 at 1:21
  • Ah, I think it's the rewritten URL (by the WordPress front-controller) that is being redirected. The issue is that the REQUEST_URI server variable is updated after the internal rewrite, so /post-exclude.html ends up being redirected to /index.php. I've updated/rewritten my answer. – MrWhite Jun 16 at 10:22
  • How did you get on with this? – MrWhite Jun 24 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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