htaccess rule to add country parameter to URL with Cloudflare geolocation - .htaccess

I've used stackoverflow extensively during my coding endeavours and thank you all for providing such a fountain of knowledge.
I would like to implement a mod_rewrite/htaccess rule that will add the visitor's country as a parameter to any URL, if the country is different from AT. Geolocation is done through Cloudflare.
Like: Visitor from CH visits https://www.example.com/blabla => https://www.example.com/blabla?country=CH
I'm not too savvy in htaccess rewrites and end up with an infinite loop / server error.
I believe it's a correct RewriteRule part that's missing for me. Here's my code:
SetEnvIf CF-IPCountry "(.*)$" Country=$1
RewriteCond %{ENV:Country} !AT
RewriteRule ^(.*)$ $1?country=%{ENV:Country} [R,L,QSA]
Any help would be greatly appreciated.

Thanks for pointing me into the right direction.
For anyone facing the same problem, the solution was:
SetEnvIf CF-IPCountry "(.*)$" Country=$1
RewriteCond %{ENV:Country} !AT
RewriteCond %{QUERY_STRING} !country
RewriteRule ^(.*)$ $1?country=int [R=308,L,QSA]
I did not find a way to append the specific country code as I couldn't reference the ENV:Country in "RewriteRule" but "int" for all non AT works well for me.

Related

Rewrite within a folder

This is very basic but still eludes me, even after studying the relevant stackoverflow pages.
I want this
mysite.net/special/
to return this (the index here)
mysite.net/info/special/
showing the requested address in the browser, not the longer actual address.
The closest I’ve got (with this in the root htaccess) is
RewriteRule ^special/$ info/special/ {QUERY_STRING}
but it’s wrong.
I had thought this is a direct match-and-replace, but apparently not.
RewriteEngine is on.
Guidance appreciated. Thank you.
RewriteRule ^special/$ info/special/ [R=301,QSA,L]
RewriteRule Flags:
R=301: redirect permanently
QSA: combine query string
L: last rule
I am assuming index as index.php for your index file. we are rewriting to your existing url.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/special/$
RewriteRule ^ info/special/index.php [L]

.htaccess baseurl -> mywebshop.nl/shop/baseurl-extension

Currently i'm working on a webshop.
The code for this webshop is stored at 1 single point.
I pointed 800 domainnames that I need to point to the location of my webshop.
This webshop is selling domainnames
So for example. My store is located on: www.mystore.com
I've a pointer from www.foobar.com
This is the function I need (works)
RewriteEngine On
rewritecond %{http_host} ^foobar.com
rewriteRule ^(.*) http://www.mystore.com/shop/foobar-com/$1 [R=301,L]
As you can see, i've used the url name as an ID.
I need this ID to get the correct product from my webshop.
How can I automate this? So how can I make sure that every url different then www.mystore.com redirects to www.mystore.com/shop/baseurl*dash*extension ?
Of course, I could type them all out but would take to much effort.
Thnx in advance.
RewriteEngine On
RewriteRule ^(www\.)?([a-zA-Z0-9]+).([a-zA-Z0-9]{3})(.*)$ http://www.mystore.com/shop/$2-$3/$4 [R=301,L]
This rule redirects:
http://www.foobar.com?id=1
to
http://www.mystore.com/shop/foobar-com/?id=1

.htaccess 301 redirect with query

I know there are tons of these questions out there, but I still don't understand my issue. Thanks to a miscellaneous link found on Google, we've found a bad link for this website. I wrote a rewrite condition for it that is the below:
RewriteCond %{QUERY_STRING} ^t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+$
RewriteRule ^/results.php$ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
The '+' are in the actual link. I've tried writing it as follows so that it doesn't count them as special characters:
RewriteCond %{QUERY_STRING} ^t=&s=BARREEL\+YOUR\+OWN\+BOURBON\+RISKY\+WHISKY\+BARREL\+PACK\+$
RewriteRule ^/results.php$ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
The site still takes me to the wrong page and does not redirect me. Can someone help with this please? Thanks in advance.
EDIT:
The URL is
/results.php?t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+
Rather than getting out RewriteRules you can use Redirect instead
Redirect 301 /results.php?t=&s=BARREEL+YOUR+OWN+BOURBON+RISKY+WHISKY+BARREL+PACK+ http://www.thepartysource.com/express/item.php?id=28952 [R=301]
However, it's worth noting, that Google has said you should Disallow/no-index search result pages on your site.

.htaccess : Rewriting initial output back into subdomain

I have the following htaccess.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www|m)\.DOMAIN\.com$
RewriteCond %{HTTP_HOST} ^([^\.]+).DOMAIN.com$
RewriteRule ^(.*)$ http://DOMAIN.com/index.php?id=%1 [NC,QSA,L,R=301]
The problem is that although it works I need it to still use the subdomain as the URL. So basically now I'm trying to rewrite DOMAIN.com/index.php?id=%1 back into SUDOMAIN.DOMAIN.com so the user sees the subdomain they requested on the address bar and not the http://domain.com/index.php?id=user
I'ver tried all sorts of things but always end up with some sort of infinite loop. Please help!
I already have a wildcard subdomain set up. In conjunction with the htaccess shown above.
The submain doesn't stick, it rewrites itself into the long string shown above. But what im trying to do is get the url ro remain subdomain.domain.com unfortunately the link you posted does peretty much the same as what I have posted above, doesn't really seem to be able to get the sub-domain to stick.
Any ideas?
Not sure if you figured it out yet, but your last line (I believe) should be this:
RewriteRule ^(.*)$ index.php?id=%1 [NC,QSA,L,R=301]
If that does not work try this:
RewriteRule (.*) index.php?id=%1 [NC,QSA,L,R=301]
No expert but I have subdomains working from some other code I found. Hope it helps.
You need to change your DNS settings before. (A record)
There is already an answer for that question.

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources