Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond for root only

         

beingk

7:28 pm on Oct 22, 2009 (gmt 0)

10+ Year Member



Hello

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

jdMorgan

10:29 pm on Oct 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi beingk, and welcome to WebmasterWorld!

Please post your current code as a basis for discussion.

Thanks,
Jim

beingk

8:36 am on Oct 23, 2009 (gmt 0)

10+ Year Member



Thanks, Jim.

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!

jdMorgan

2:45 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While defined in PERL and other languages, $0 is undefined in mod_rewrite. Also, although it may have been introduced by modifying your code to comply with our TOS, there's an error in the logic here, in that the hostname tested by the 2nd RewriteCond does not match the redirect's target hostname. I'd suggest:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(UKŠES)$
RewriteCond %{HTTP_HOST} !^(http://europe\.example\.com)?$
RewriteRule ^/(.+)$ http://europe.example.com/$1 [R=301,L]

I removed the [NC] from the hostname RewriteCond so that case errors will be corrected, added a provision to prevent infinite looping on HTTP/1.0 requests with blank Host headers, corrected the back-reference to $1, and changed the regular-expressions pattern to require at least one character in the URL-path.

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

beingk

4:55 pm on Oct 23, 2009 (gmt 0)

10+ Year Member



Thanks, Jim. Looks like quite a few things changed. However, I still can't get it to work. I'm also using an IP currently, so maybe I'm missing something here in the syntax.


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]

jdMorgan

6:18 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, the hostname in the RewriteCond pattern and the RewriteRule substitution must match, excepting the negation operator, anchoring, and required character-escaping in the RewriteCond pattern.

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

jdMorgan

6:26 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I may have gotten the logic of the URL-path pattern backwards. If you want to redirect only home-page/domain-root requests, then the RewriteRule pattern should be either "^/$" in a config file outside of any <Directory> container, or "^$" otherwise. You should also drop the $1 completely, since it will always be blank if the rule matches.

Jim

beingk

11:39 am on Oct 24, 2009 (gmt 0)

10+ Year Member



It works!

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.

jdMorgan

2:48 pm on Oct 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just want to give such "id=" requests a free pass through this rule, add another RewriteCond:
 RewriteCond %{QUERY_STRING} !^([^&]*&)*id=. 

Jim

beingk

3:47 pm on Oct 24, 2009 (gmt 0)

10+ Year Member



Thank you!

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]

lasares

11:45 pm on Dec 18, 2009 (gmt 0)

10+ Year Member



@jdMorgan: Thank you so much. I had been struggling for hours with the same problem beingk had. I read and reread everything I knew or could find on .htaccess and url rewriting, to no avail.

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 !

jdMorgan

1:29 am on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lasares,

Well, you'll be happy to know that there's really no down-side to registering here. It's required only so we can determine that the poster is a human, and not a spam-bot... Seriously! :)

Jim

g1smd

1:47 am on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since search engines 'come from the US' none of your other content will ever be indexed.

An important point to bear in mind.