1

I need to add following Apache redirect rules to .htaccess:

http://www.example.com to be redirected to http://www.onemoreexample.com

but http://www.example.com/support should not be redirected.

To achieve this, I added the redirect rules below:

 RewriteCond %{REQUEST_URI} !^/support/
 RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

But http://www.example.com/support/ is taking me to http://www.onemoreexample.com/support/ after adding the above rule

Following rules also exist before the above rules to redirect to www.

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

My .htaccess file looks like this

   RewriteEngine On
   RewriteBase /
   RewriteRule ^index\.php$ - [L]

   # add a trailing slash to /wp-admin
   RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^ - [L]
   RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
   RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
   RewriteRule . index.php [L]

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

   RewriteCond %{REQUEST_URI} !^/support/
   RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

Please suggest how to resolve this.

  • Have you cleared your browser cache? Do you have any other directives in your .htaccess file? Is /support a physical directory? Any other .htaccess files? – MrWhite Jun 27 at 9:34
  • On the face of it there is nothing wrong with the directive you posted, so there must be something else affecting this. We need more information about your config... other Apache / .htaccess directives and filesystem structure. – MrWhite Jun 27 at 14:36
  • There is a rule to redirect to www above this rule . I have added this info in original description – Zama Ques Jun 27 at 14:50
  • And what is /support? How is this routed to return a response? To confirm... /support is not being redirect, but /support/ is? – MrWhite Jun 27 at 14:57
  • 1
    "Yeah...It is /support/" - what do you mean by this? So, is the request for /support or /support/ or both and neither, both or one or the other is redirected? "It is an extension of wordpress multisite feature" - Ah, so you must have other WordPress related directives that route the request. Please include your entire .htaccess file. – MrWhite Jun 27 at 16:18
1

In isolation, the redirect directives themselves are OK, so there must be something else going on...

Having looked at your .htaccess file as a whole, with the redirect directives in-place, I'm surprised your redirect directives are actually doing anything(?!), since you've put them in the wrong place at the end of the .htaccess file, after the WordPress multisite directives.

Since there is no physical directory (or file) called /support then WordPress rewrites this request to /index.php before your redirect occurs and processing stops before your redirect is even processed. (So, how this is seemingly redirecting the request is not clear - the obvious guess would be that you are seeing a cached redirect (after some earlier testing perhaps) - or there is something else entirely that is triggering the redirect?)

Generally, external redirects should always go at the top of the .htaccess file, before any rewrites. So your .htaccess file should look something like the following instead:

RewriteEngine On
RewriteBase /

# External redirects...
RewriteCond %{REQUEST_URI} !^/support/
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

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


# WordPress multisite rewrites...
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

You will need to ensure the browser cache is clear before testing.

UPDATE#1: It resolves but the CSS breaks

The above will also redirect any static resources (CSS, JS, images) that are not also referenced with the /support/... URL-path prefix. If the corresponding CSS files are not also located at the redirected domain then your CSS will break. This maybe what's happening here... (but, assuming these are shared/common CSS resources I would have expected this to break for all the redirected pages as well? Unless perhaps the CSS used by the /support/ page is unique?).

To exclude CSS and other static resources you would need to add another exception. For example:

# Redirect everything except "/support/..." and other static resources
RewriteCond %{REQUEST_URI} !^/support/
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|gif|png)$
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

UPDATE#2 one more issue... http://www.example.com/support/wp-admin should not be redirected too but it is getting redirected to http://www.onemoreexample.com/wp-login.php.

The issue here is that /support/wp-admin is first being redirected to /wp-login.php (no /support/ prefix) by WordPress on the same domain which will then get redirected to your other domain with the directive above. So, you will need to make an exception for /wp-login.php as well, if you don't want unauthenticated users that visit /support/wp-admin being redirected.

For example, change the first condition to read something like the following instead:

RewriteCond %{REQUEST_URI} !^/(support/|wp-login\.php$)
  • Thank you so much. I will test it after sometime – Zama Ques Jun 27 at 21:30
  • Just an observation... It actually works If I put the redirect rules after RewriteRule ^index\.php$ - [L] in .htaccess but it breaks the CSS formatting of /support. If I keep the rules as in answer section , /support continues to redirect to onemoreexample.com – Zama Ques Jul 3 at 5:25
  • 1
    It seems CSS are loading fine now . We are testing further . Thanks so much. You are Awesome! – Zama Ques Jul 4 at 16:57
  • 1
    It would seem to be the /wp-login.php URL that is being redirected (because WP itself first redirects unauthenticated users to login), but you've included an exception for /support/wp-admin/wp-login.php/ in the condition - so I can't see that this would ever match? I would assume you need to exclude /wp-login.php? I've updated my answer. – MrWhite Jul 16 at 9:43
  • 1
    The pipe symbol is used to express alternation (ie. logical OR) in a regular expression (regex). For example, the regex (support/|wp-login\.php$) matches either support/ OR wp-login.php. (The $ in the second part indicates that nothing can come after wp-login.php.) It's the same regex syntax that PHP, JavaScript and other languages use. – MrWhite Jul 17 at 17:56
0

Add a /? to consider optional trailing slash as well in rule.

RewriteCond %{REQUEST_URI} !^/support/?
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]
  • Tried but no luck – Zama Ques Jun 27 at 8:58
  • With the regex ^/support (with no trailing end-of-string anchor) everything after the pattern is already optional. – MrWhite Jun 27 at 9:36

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.