Reputation: 389
I need to redirect all users who don't have an 'allowed' IP-address to another subdomain. The users should be redirected to the other subdomain with the same URL.
For example:
dev.example.com/contact_us.php
=> www.example.com/contact_us.php
This part is now working with the following code in my .htaccess file:
RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteCond %{HTTP_HOST} ^dev\.example\.(.*)$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
Besides that the domain can have different domain-extensions like: .com .net .pl .se
That means if the user try to navigate to dev.example.se/contact_us.php
and don't have an 'allowed' IP-address - then the user should be redirected to www.example.se/contact_us.php
I can't figure out how to make the second part working.
Upvotes: 0
Views: 108
Reputation: 786041
You can capture last of domain name from RewriteCond
itself and use a back-reference later in RewriteRule
like this:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteCond %{HTTP_HOST} ^dev\.example\.(.+)$ [NC]
RewriteRule ^ https://www.example.%1%{REQUEST_URI} [L,R=301,NE]
Take note of capture group (.+)
in RewriteCond
that captured value after dev.example
from host name. Then we use a back-reference of %1
later in RewriteRule
that replaces same value.
Note that you may also use this rule where example
is not repeated again:
RewriteCond %{REMOTE_ADDR} !^10\.0\.1\.1$
RewriteCond %{HTTP_HOST} ^dev\.(example\..+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Upvotes: 2