Issues with redirect rules to redirect all URLs except one page
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, wordpress, apache, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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.
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-adminshould not be redirected too but it is getting redirected tohttp://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$)
Add a /? to consider optional trailing slash as well in rule.
RewriteCond %REQUEST_URI !^/support/?
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]
Comments
Post a Comment