Ignore magento admin directory in GeoIP Htaccess Rewrite - .htaccess

I've got a magento install that is using the htaccess mod_geoip to redirect people from specific countries to other store fronts. It's working like a charm (after I got it to ignore javascript and skin files), but I'm going to be having someone from Australia input some orders into the backend of magento.
The issue is that they can't access the main backend at store/index.php/admin (they are rewritten to austore/index.php/admin) and when they go to put orders in, they are missing some integral components of the order process which I think is due to the url being rewritten to austore/index.php/admin. Wondering if there is a way that if the request uri is store/index.php/admin that they won't get rewritten. My code is below from my htaccess:
RewriteRule ^(skin|js) - [L,NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(AQ|AU|MY|BV|BN|BN|MM|KH|CN|CX|CC|CK|GQ|FJ|PF|GU|GW|HM|HK|ID|KI|KR|KP|KR|LA|MO|MY|MH|FM|MM|NR|NC|PG|NZ|NU|NF|PG|CN|PH|PN|WS|SG|SB|KR|LK|BN|TW|TW|AU|TH|TL|TK|TO|TV|VU|VN|VN|WF)$
RewriteCond %{REQUEST_URI} ^/store(/.*)$ [NC]
RewriteRule ^ /austore%1 [L,R]
Thanks for the help!

Taken from this, http://www.sonassi.com/knowledge-base/magento-kb/secure-your-magento-admin/ you can use the same rewrite logic in what you want to do.
RewriteCond %{REQUEST_URI} !^/store/(index.php/)?(admin|custom_extensions_go_here)/ [NC]
RewriteCond %{REQUEST_URI} !^/store/downloader/ [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(AQ|AU|MY|BV|BN|BN|MM|KH|CN|CX|CC|CK|GQ|FJ|PF|GU|GW|HM|HK|ID|KI|KR|KP|KR|LA|MO|MY|MH|FM|MM|NR|NC|PG|NZ|NU|NF|PG|CN|PH|PN|WS|SG|SB|KR|LK|BN|TW|TW|AU|TH|TL|TK|TO|TV|VU|VN|VN|WF)$
RewriteCond %{REQUEST_URI} ^/store(/.*)$ [NC]
RewriteRule ^ /austore%1 [L,R]
Just bear in mind, doing this through .htaccess could potentially cause some bizarre issues in the long run with 3rd party extensions, that have custom admin routes (that are not prefixed with /admin) break. So just be mindful of this.
I would advocate the use of a Magento extension (PHP based), mod_geoip store switcher - as then you can make it apply only to the frontend area of the Magento store (to prevent the potential errors I've described above)

Related

htaccess redirect, everything is redirecting instead of specific requests

I'm trying to setup a new project structure. Due to some limitations of my cloud provider, I need to do some htaccess magic which I'm struggling with.
The project structure is as following
/docroot => Contains my Drupal installation
/docroot/frontend => Contains an Angular frontend
/docroot/pim => Is a symlink to /docroot.
What we need is that when we surf to example.com/pim that it redirects to the Drupal docroot /docroot. Since due to limitations of the cloud provider we can't put it in the /pim folder, they suggested to make a symlink.
Any other request to example.com should go to docroot/frontend.
So I've added the following code in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteRule !^pim($|/) http://example.com/frontend%{REQUEST_URI} [L,R=302]
Current behaviour is that all requests go to /frontend/index.php (I think the index.php comes from the .htaccess of Drupal, so the redirect is not done cleanely) While I would expect a request to example.com/test to direct to example.com/frontend/test
Also example.com/pim points to example.com/frontend/index.php instead of staying in the /pim directory
I've been able to fix it. :)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteCond %{REQUEST_URI} !^/pim.*$
RewriteRule ^ frontend%{REQUEST_URI} [L,R=302]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/frontend.*$
RewriteRule ^ %{REQUEST_URI} [L]

How to redirect root domain to subfolder (with https) and rest of addon domains to subfolders (without https)

How to redirect root domain to subfolder (with HTTPS) and rest of other addon domains to subfolders (without HTTPS).
Currently I have this .htaccess in root which redirects with HTTPS to the-main-subfolder ok. But my other addon domain, say domain2 also gets redirected to the-main-subfolder.
I would like to redirect domain2 to the-domain2-subfolder without HTTPS.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch ^/$ /the-main-subfolder/
I am not sure if this code is correct as it might me using a wildcard. I got this code from searching on net but there are so many suggestions that I am confused now!
In summary: My main hosting account in root should go to https://www.domain1.co.uk/the-main-subfolder when user types in domain1.co.uk in browser and my addon domain http://domain2.co.uk should go to http://www.domain2.co.uk/the-domain2-subfolder.
You can use additional RewriteConds to define specific redirections:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^example1\.org$
RedirectRule ^(.*)$ /example1\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^example2\.org$
RedirectRule ^(.*)$ /example2\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host1\.example\.org$
RedirectRule ^(.*)$ /host1\.example\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host2\.example\.org$
RedirectRule ^(.*)$ /host2\.example\.org-subfolder/$1 [L,QSA]
I added a few examples to demonstrate the redability of explicit implementation and that you can do that for both, separate domains and hostnames (sometimes incorrectly called "subdomains"). I would always prefer such explicit notation over generic approaches since you can individually modify things, for example for testing or debugging purposes. Except if you are in a mass hosting situation obviously, then a database based approach makes sense.
Note that the redirection for what you call the "root domain" (example.org here) has a second RewriteCond now. Both conditions are AND-combined per default.
For safety you probably also want to add some more rules to redirect requests to something like https://example.org/host1.example.org-subfolder to the specific domain name, since according to your description you are limited to a single file tree in your hosting account. Same for request to http://test1.example.org/test1.example.org-subfolder/... to eliminate the literal folder name.
Oh, and a warning: the above syntax works for .htaccess style files only. If you have access to the real host configuration then you should always prefer to place such rules in there. However you need a slightly changed syntax then. .htaccess style rules are notoriously error prone, hard to debug and they really slow down the http server. They are only offered as a last option for those without access to the host configuration.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain1.co.uk$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
Thanks #arkascha - Everything now works as expected with the above code. I suppose we do not need to mention so called add-on domains here at all because cPanel handles the sub-directories for them internally when we add subsequent domains on the hosting package (i.e. addon domains)!
Just to update that my previous solution partially works as it has few niggles/bugs. So went back to the drawing board and suddenly realised I was unnecessarily trying too hard!!
Deleted the old htaccess file first and followed instruction below..
The solution is already provided by cPanel in something called "Redirects" in Panel Icons.
I just had to enter everything in user interface text boxes like choose domainname = "domain1", old folder = "\", new folder = "https://www.domain1.co.uk/the-main-subfolder" - And just click create the redirect. In doing so it creates a .htaccess file itself automatically. I am sharing this below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.co\.uk$
RewriteRule ^/?$ "https\:\/\/www\.domain1\.co\.uk\/the-main-subfolder\/" [R=301,L]

htaccess on one.com webspace

Recently I moved my websites to the hoster one.com. They have setup an automated mechanism (I dunno what they use to achieve that) to rewrite any first-level folder on the webspace to a subdomain.
I.e. the folder http://example.com/folder1/ will be also available as http://folder1.example.com/
Now, I have a site, that is using quite a lot javascript to include pages from a hardcoded, static source. Due to the SOP the scripts are working depending on which hardcoded reference they use.
So, to make sure that everybody gets a working version of the website, i wanted to redirect the direct folder access to the subdomain as well.
My htaccess for this - which is working localy and on various htaccess-testers out there - seems to be not working with one.com:
RewriteEngine On
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder1.*?$ [NC]
RewriteRule .* http://folder1.example.com/ [R=301,L]
Since I don't know the exact mechanism one.com is using to achieve the mentioned behaviour it might just be a conflict with my rules.
Support says, that all the used commands are fully supported, and therefore wasn't be able to tell what's going wrong...
Does anybody have encountered something similiar and has a hint for me?
just fiured out the solution:
RewriteEngine On #does not work
vs.
RewriteEngine on #does work
You need to check that the actual request was made for /folder/ and not the URI (which can internally be rewritten). Try:
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /+folder1/ [NC]
RewriteRule ^folder1/(.*)$ http://folder1.example.com/$1 [R=301,L]

Use .htaccess to load files from another folder

I have a good reason to do this. I feel this is the most cost effective way of providing an update.
This is my current url structure
/ <-- Contains Website
/cart
/cms
Boss wants the client to have an option to forego the website and instead load the cart in place of the website. The system is fairly old, the website and cart are completely different systems. We host a lot of websites from this single system. Configuring 2 systems for 2 different websites types will involve too much maintenence.
The least work solution would be to rewrite the /cart into /. I came up with the following.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/cart/
RewriteCond %{REQUEST_URI} !^/cms/
RewriteRule ^(.*)$ cart/$1
This returns a server error. Adding /cart/$1 on the final line does work. I need this to work when the system isn't installed in the root directory. Does anyone know how to fix this?
You can use this rule:
RewriteEngine On
# Determine the RewriteBase automatically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_URI} !/(cart|cms)/ [NC]
RewriteRule ^(.*)$ %{ENV:BASE}cart/$1 [L]

how to rename a module in the url with mod_rewrite

I'm using MVC with /<module>/<controller>/<action>/ have a module at example.com/module/whatever, and I need to 'rename' it to example.com/module-a/whatever. The whole application is already written, so I can't go through and change it everywhere in my code, so I'm hoping to do it with mod_rewrite. I've tried the following
RewriteCond %{THE_REQUEST} ^GET\ /module/
RewriteRule ^module/(.*) /module-a/$1 [L,R=301]
which did what I wanted as far as redirecting all urls like example.com/module/whatever to example.com/module-a/whatever, but now I need all requests at 'module-a' to be internally rewritten as 'module'. It also needs to work for the module root (i.e. example.com/module with no trailing slash). Is this possible? I added
RewriteRule ^module-a/(.*)$ module/$1
directly beneath the above condition and rule, but when the page is accessed, it still says the module 'module-a' is not found.
Edit:
I have a few more rules below those, I wouldn't think they would affect this, but here they are anyway:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Solution
I ended up using
RewriteCond %{THE_REQUEST} ^GET\ /module/
RewriteRule ^module$ /module-a [L,R=301]
RewriteRule ^module/(.*) /module-a/$1 [L,R=301]
to redirect all links from module to module-a. I had to do it with 2 rules because I don't know regex well enough to combine them, handling the special case of the url example.com/module.
To rewrite internally, the original rule I had would normally work, but Zend seems to do some stuff that overrides that, so I had to handle it with routes. See rename a zend module with routes
If I understand correctly then you've gone about this from the wrong direction. I am also not clear on the purpose of your RewriteCond
You want all module-a/* requests to be processed internally as module/*, so all you need is a simple rewrite::
RewriteRule ^module-a/?(.*) /module/$1 [L]
I suspect the problem you are having is the internal links on the site all reference /module/ rather than /module-a/, but putting a 301 there will cause no end of problems (not least with search engines), and with the subsequent rewrite you may fall into circular references. You are much better off changing the link code in your app (if you have a link abstraction class), or at worst using output buffering to swap all links out before rendering the page.
Note: The second rule below the above is not being processed if the first matched, as [L] causes mod_rewrite to cease processing if that rule is matched.

Resources