Forum Moderators: phranque
I looked at a similar post but it didn't seem to answer my question. Basically I'm using GeoIP to redirect users to different subdomains.
Let's say I have a main website http://example.com for the US. And there's a website for Europe too, so that when people from Spain visit http://example.com they get redirected to [europe.example.com....] This is straightforward.
The problem is when I want the visitors to be able to view pages on http://example.com, just NOT the main page. So if they request anything after a trailing slash they should be able to stay on the domain.
So in my mind this translates to:
If requests from Europe and to http://example.com (a trailing slash at most)
Then redirect
If requests from Europe and to http://example.com/whatever
Then NO redirect
Thanks in advance for any input.
k
Here we go:
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^http://example\.com$ [NC]
RewriteRule ^(.*)$ http://europe.example.com$0 [R=301,L]
If I type in http://example.com I end up at [europe.example.com...] (good)
If I type in http://example.com/something I end up at [europe.example.comsomething...] (not good). I don't want to be redirected to the subdomain if I'm requesting a page other than the home page.
Thanks!
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^(http://europe\.example\.com)?$
RewriteRule ^/(.+)$ http://europe.example.com/$1 [R=301,L]
Note that posting on this forum changes the solid pipe character to a broken pipe "Š" character; You must replace this broken pipe character with the correct solid pipe before using any code snippets you may find here.
Jim
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^(http://209\.1\.nnn\.2/)?$
RewriteRule ^/(.+)$ http://away.example.com/$1 [R=301,L]
Basically no redirect happens when I try http://209.1.nnn.2/, even if I disable the first condition.
[edited by: jdMorgan at 6:18 pm (utc) on Oct. 23, 2009]
[edit reason] Obscured IP address [/edit]
Where is your code -- .htaccess or a server config file? If the latter, is it within a <Directory> section?
If in a server config file outside of any <Directory> section, then the rule pattern must start with a slash, as you have now. Otherwise, the rule pattern must not start with a slash, as it won't be present in the requested URL-path, and the pattern will therefore never match.
Do you have any other working rules? If not, you will need to put either both of these lines or only the second of them ahead of your RewriteRule section, as determined by your current server configuration (I can't tell) :
Options +FollowSymLinks
RewriteEngine on
Jim
Here's my final set of rules in a .htaccess file:
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^(http://example\.com)?$
RewriteRule ^$ http://europe.example.com/ [R=301,L]
One last thing: non-home-pages may be requested using this format -- http://example.com/?id=#*$!, but the current rules still treat this as a home-page request, which is incorrect. Is it possible to handle this?
Thanks again for your help.
The final code, for completeness sake:
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^(http://example\.com)?$
RewriteCond %{QUERY_STRING} !^([^&]*&)*q=.
RewriteRule ^$ http://europe.example.com/ [R=301,L]
[edited by: jdMorgan at 4:37 pm (utc) on Oct. 24, 2009]
[edit reason] Disabled smilies in code. [/edit]
You and only you provided the answer : the RewriteRule pattern should be either "^/$" in a config file outside of any <Directory> container, or "^$" otherwise.
After my darn afternoon, i did not not know if I should rejoice or weep, but I did one thing : I registered to this forum for the sole purpose of thanking you.
Have a nice week-end !