Reputation: 11
Please help I've tried hard but it just doesn't work.
What i'm trying to accomplish is simply redirect all users within our company network (all with the same external ip address, let's say 192.168.0.1) to htp://start.example.com/page_2 when they visit htp://start.example.com/page_1. All other ip addresses should be just fine visiting htp://start.example.com/page_1.
Ive tried these 2 rules:
1:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.1$
Redirect 301 /page_1 http://start.example.com/page_2
In this case the redirect only works when the ip check is disabled.
2:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.1$
RewriteCond %{REQUEST_URI} !^page_2$
RewriteRule .* http://start.example.com/page_2 [R=301,L,NC]
In this case the redirect only works when the path check is disabled.
What is wrong?
Oké, found the solution, Somehow the position of the redirect just didn't work, i moved it to the top of the document and now it works with the following syntax. In the example I've added an extra IP check and a User agent "Windows" check.
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.1$ [NC,OR]
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.1$ [NC]
RewriteCond %{HTTP_USER_AGENT} Windows
RewriteRule page_1$ http://start.example.com/page_2 [R,L,NC]
Upvotes: 1
Views: 2325
Reputation: 74108
The first one is a mix of two unrelated directives RewriteCond
from mod_rewrite and Redirect
from mod_alias.
The RewriteCond
has no associated RewriteRule
.
The second one cannot work, because REQUEST_URI
always contains the leading slash. The proper condition would be
RewriteCond %{REQUEST_URI} !^/page_2$
The rule rewrites anything (.*
) to page_2
, not just page_1
. To restrict this properly, this should be
RewriteRule ^page_1$ http://start.example.com/page_2 [R,L,NC]
In case there are other redirects, e.g. adding a path at the end, the condition should reflect this this too by removing the end of string marker $
RewriteCond %{REQUEST_URI} !^/page_2
This matches anything starting with /page_2
.
You may also exclude other paths from redirection, separated with a vertical bar
RewriteCond %{REQUEST_URI} !^/(page_2|page_3)
Finally, never test with R=301
!
Upvotes: 1