1

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]
nielsvoo
  • 11
  • 4
  • Oh, i know the links in the text are htp instead of http, this was done to post this question, there was a check build in to prevent users using to many links. not really helpful in this case. – nielsvoo Jul 26 '17 at 14:20
  • I think you should check out a different stackexchange (https://serverfault.com/) that I believe are more focused on these sorts of questions. – Amndeep7 Jul 26 '17 at 14:56

1 Answers1

1

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!

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Ha Olaf, thanks you for your reply, I've tried your solution but now the page_2 is not displayed because of to many redirects… – nielsvoo Jul 27 '17 at 08:10
  • @nielsvoo This shouldn't happen, because `page_2` is excluded by this condition. Maybe there are other rules/redirects causing this. Is `page_2` a real static file or maybe anything else, like a directory? – Olaf Dietsche Jul 27 '17 at 15:19
  • Hi Olaf, page_2 is a path alias on a Drupal 8 site. There are some redirects in the .htaccess file, all done by Drupal self. – nielsvoo Jul 27 '17 at 20:50
  • @nielsvoo Please see updated answer, but maybe this is more than what SO can provide. – Olaf Dietsche Jul 28 '17 at 07:21
  • Hi Olaf, thanks for all the help the location was one of the problems, I've moved it to the top and now it works well. See the update above. – nielsvoo Jul 28 '17 at 11:30
  • @nielsvoo If I had asked your question and gotten Olaf's help, I would have also given him credit for giving you the preferred answer. Maybe there's a check mark on you screen you can click? (He already has many multiples of my paltry points, but he's done the work for more!) One vote from the cheap seats! – BaldEagle Jul 05 '18 at 06:04