How to overide the geo IP Redirect - .htaccess

I currently have a Geo-redirect set up in my htaccess to redirect user say from Asia who visits example.com to asia.example.com. I am now wondering how to override that redirection so users from asia.example.com can get to example.com through a link on asia.example.com.
Is that possible or do I have to use a client-side redirect and how would I go about setting that up.
And some background info example.com is running Magento and asia.example.com will be a landing page for now.
Thanks
Update: My host says I should install this https://github.com/maxmind/GeoIP2-php on Magento... How would I do that?
Update from comment:
# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(VN|SG|CN|MY|JP|KP|KR)$
RewriteRule ^(.*)$ asia.example.com$1 [R,L]

Normally Magento offers a store switcher in the form of a select field, see it working on demo.magentocommerce.com. The theme in use might change how this is displayed, it could be a link or button instead but the principle is the same, a store cookie is written with the store code as it's value. We can test for that directly in .htaccess and avoid GeoIP if present.
# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(VN|SG|CN|MY|JP|KP|KR)$
# New cookie condition goes here
# ! means true when it doesn't match
# \b means word boundary
RewriteCond %{HTTP_COOKIE} !\bstore=default\b
RewriteRule ^(.*)$ asia.example.com$1 [R,L]
It is necessary for the cookie to be transmitted on every request or the user might find themselves being redirected later. You may have to set the cookie domain to .example.com (note the leading period) if it misbehaves. You can do this in Magento's admin under System > Configuration > Web > Session Cookie Management. This allows the cookie to be written by asia.example.com and be read by example.com, and vice versa.
If you want to make a link instead of the store switcher then use Mage::getUrl in this way, it will generate an URL with a ___store query parameter:
<?php
$url = Mage::getUrl('', array(
'_store' => 'default',
'_store_to_url' => true
));
?>
<?php echo $this->__('International Store') ?>

Related

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

Redirecting parts of a web page in IIS

I have a "dummy" web site in IIS on a 2008 server that redirects users to the correct web site.
For historical reasons this box, and what everyone knows, is "mail.abc.com".
"mail" has now come to mean several other things, but people just type that name or have it bookmarked and expect to get directed to the "type of service" they are looking for.
mail.abc.com (ports 80 or 443) actually redirects to -> webmail.abc.com (port 443)
webmail contains a webportal for the users to sign into their email, but it also holds their personal web pages. These are accessed by going to ->mail.abc.com/~their_name
I want to create some type of rule that splits this into two pieces (we are splitting these two services onto 2 servers - one for the email part and one for their personal web pages)
so if you go to the generic page the redirect should goto webmail.abc.com
but if there is a "~", I want to redirect to a different location => webpages.abc.com/~their_name
I was looking at HTML redirects to do this, but I'm not sure how to split the typed-in URL, the "root" URL could be easily redirected, but how would I build something for any URL that had the tilde in it?
If you are able to use .htaccess, put a file .htaccess with the following configuration in the root folder of the mail.abc.com host:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/~(.*)
RewriteRule ^(.*)$ http://webpages.abc.com/~%1 [R=302,L]
RewriteCond %{REQUEST_URI} ^(.*)
RewriteRule ^(.*)$ http://webmail.abc.com%1 [R=302,L]
What happens:
Line 2 and 3 is the first set of RewriteCond and RewriteRule. The RewriteRule in the third line will redirect to http://webpages.abc.com/ only, if mail.abc.com was called with ~ in the URL segment. So if I try to open for example http://mail.abc.com/~m_eberhard I will be redirected to http://webpages.abc.com/~m_eberhard.
The next set in line 4 and 5 will redirect everything else to webmail.abc.com. If I try to open mail.abc.com/asdasdasd I will be redirect to webmail.abc.com/asdasdasd

mod_rewrite rewrite url using .htaccess

I have searched on google and here for a tutorial to help me rewrite my urls.
I would like somebody to explain what I must write and why.
I have this url:
http://iescup.eu/tournaments.php?tourney[id]=1
http://iescup.eu/tournaments.php?tourney[id]=2
http://iescup.eu/tournaments.php?tourney[id]=3
and so on
I would like to have this url:
http://iescup.eu/#!/tourneys/1
http://iescup.eu/#!/tourneys/2
http://iescup.eu/#!/tourneys/3
and so on
Sincerely
Rune Naundrup Dahl
So you type http://iescup.eu/tournaments.php?tourney[id]=1 in your browser's URL address bar. The request /tournaments.php?tourney[id]=1 gets sent to the server iescup.eu. On that server, these rules in the htaccess file in the document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tourney\[id\]=([0-9]+)$ [NC]
RewriteRule ^/?tournaments.php$ /#!/tourneys/%1? [L,R=301,NE]
The %{QUERY_STRING} variable is matched against and the numeric ID is grouped and backreferenced by %1. The rule's target has a ? at the end to remove the query string and the NE flag is used so the # doesn't get encoded.
The rule redirects the browser to http://iescup.eu/#!/tourneys/1 thus changing the URL address bar. The browser then sends another request to iescup.eu, /. Note that the #!/tourneys/1 fragment is never sent to the server. Fragments are client side only and is used to determine how content should be dealt with (also used by javascript).

Htaccess alias redirect fake multilanguage url

I have a domain www.domain.com with this kind of url
www.domain.com/my-italian-page.html (already rewritten by others htaccess rules)
I'd like to create a fake multilanguage url like
www.domain.com/my-english-page.html
The user will see in the address bar the rewritter url www.domain.com/my-english-page.html but the content that I'd like to show is the original www.domain.com/my-italian-page.html .
I'm on a shared server so I can't use apache vhost rule so I have to find a solution via htaccess.
Someone could help me to find the right way?
Thanks
So you want english URLs pointing to italian content? Hope your php script that generates these rewrite rules does the translating. But you'd do this for each one of your pages:
RewriteRule ^/?english-page.html$ /italian-page.html [L]
for each one of your pages.
Generally you want to keep the code executed in the web server as small as possible, so having a rewrite rule for each page is not usually a good idea. I suggest to implement this the way most CMS work when SEO URLs are enabled:
Rewrite any url (mydomain/anytext.html [actually you should not use the .html extension either]) to a script (eg. mydomain.tld/translate.php)
Use content of $_SERVER['PATH_INFO'] (should contain anytext.html) to display the correct page
Set correct HTTP response code if page does not exist: http_response_code(...) (see end of this answer for a function on php5 below 5.4: PHP: How to send HTTP response code?)
Sample .htaccess (actually originally "stolen" and severely modified from a typo3 setup)
RewriteEngine On
# Uncomment and modify line below if your script is not in web-root
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) translate.php$1 [L]
Very basic pseudo-code-like (not tested, there may be syntax errors) example:
<?php
// using a database? you have to escape the string
$db = setup_db();
$page = db->escape_string(basename($_SERVER['PATH_INFO']));
$page = my_translate_query($page);
// no database? do something like this.
$trans = array( 'english' => 'italian', 'italian' => 'italian' );
$page = 'default-name-or-empty-string';
if(isset($_SERVER['PATH_INFO'])) {
if(isset($trans[basename($_SERVER['PATH_INFO'])])) {
$page = $trans[$trans[basename($_SERVER['PATH_INFO'])]];
}
else {
http_response_code(404);
exit();
}
}
// need to redirect to another script? use this (causes reload in browser)
header("Location: otherscript.php/$page");
// you could also include it (no reload), try something like this
$_SERVER['PATH_INFO'] = '/'.$page;
// you *may* have to modify other variables like $_SERVER['PHP_SELF']
// to point to the other script
include('otherscript.php');
?>
I saw in your answer that you have another script - dispatcher.php - which you seem reluctant to modify. I modified my response accordingly, but please keep in mind that by far the easiest way would be to just modify the existing script to handle any English paths itself.

.htaccess - rewriting and then hiding part of the url?

I have the following RewriteRule in my .htaccess:
RewriteRule ^products/([A-Za-z0-9-\s\#]+)/([A-Za-z0-9-\s\#]+)/?$ /store/products/product.php?prod=$1&src=$2 [L,QSA]
It takes a url such as:
http://example.com/store/products/lawnmower/blogThatLovesUs
and shows the page here:
http://example.com/store/products/product.php?prod=lawnmower&src=blogThatLovesUs
Is there a way I can edit this RewriteRule so that the user just sees
http://example.com/store/products/lawnmower/(sans blogThatLovesUs)
in their address bar?
Sure, you can do that, but you will lose your affiliate's ID (blogThatLovesUs) in the process. You could use your original URL to redirect as you are doing, and then have the resulting page (product.php) cache the affiliate value in session and then do a standard redirect in PHP (with header()) to the updated URL without the affiliate value. Does that make sense?
I want to illustrate to be more clear:
URL: http://photojojo.com/store/products/lawnmower/blogThatLovesUs
through RewriteRule you end up with:
URL: http://photojojo.com/store/products/product.php?prod=lawnmower&src=blogThatLovesUs
At this point, in product.php you store blogThatLovesUs off in session somewhere and then:
<?php
header("Location: http://photojojo.com/store/products/lawnmower/");
?>
To get the browser to the URL you want them at.

Resources