Redirect loop in htaccess - .htaccess

why is this causing a redirect loop? How do I have to change the code, to make it work?
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteRule ^$ http://website.com/?___store=german
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteRule ^$ http://website.com/?___store=dutch
Thank you,
Toby

RewriteEngine On
RewriteCond %{QUERY_STRING} !\b___store=\w+\b
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteRule ^$ /?___store=german [L,QSA]
RewriteCond %{QUERY_STRING} !\b___store=\w+\b
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteRule ^$ /?___store=dutch [L,QSA]
You don't need the http://website.com. .htaccess files loop so adding [L] isn't good enough; you need to detect the loop and looking for the store parameter is a good way. You also need the [QSA] flag if some requests use additional params.

Try this:
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=german [L]
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=dutch [L]
If you go to website.com with AL of 'de', and then you get redirected to __store=german, your AL will still be 'de', so it will keep trying to redirect to that __store=german. Adding the [L] flag will stop apache from attempting to redirect multiple times.
This is another option, although the ___store parameter would have to be the same as the accept language. I think this should work (not exactly sure on the specifics of passing variables from a condition)
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} (de|nl) [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=%1 [L]

Related

RewriteCond for multiple RewriteRules

currently, I have the following .htaccess-file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^ index.php?file=index [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^language/?$ index.php?file=language [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^about-us/?$ index.php?file=about_us [NC,L,QSA]
As you can see, I have to copy the following 2 lines for every RewriteRule:
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
This works, but I want to know if there is a better solution?
What I want?
For example, when a user visits http://{subdomain-here}.example.com/about-us, the page index.php?file=about_us must show up. For every subdomain, except for the subdomains javascript, css and images.
Edit 1: If a visitors goes to http://javascript.example.com/about-us, the browser should give a 404 error.
Edit 2: a "general" regex (e.g. (.*)) will not work because the name of the file in the URL is not always the same as the original filename. http://subdomain.example.com/about-us should point to index.php?file=about_us (see the _) and not to index.php?file=about-us.
You can apply the opposite logic
RewriteEngine on
# Don't touch anything when it comes to those 3 subdomains
RewriteCond %{HTTP_HOST} ^(?:javascript|css|images)\.example\.com$ [NC]
RewriteRule ^ - [L]
# If we reach this part, this means we're not in the 3 subdomains restriction
RewriteRule ^$ /index.php?file=index [NC,L]
RewriteRule ^language$ /index.php?file=language [NC,L]
RewriteRule ^about-us$ /index.php?file=about_us [NC,L]
You can see that as an equivalent to
if subdomain in (javascript, css, images)
return
do something here for other subdomains
You can use this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]
or the following
:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(javascript|css|images) [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]

htaccess mod-rewrite language overrides

I am using some mod-rewrite code in my sites .htaccess file to redirect users with browser languages set to various languages to different versions of my site. This is working well - but... I need a way to be able to override these rewrite rules when a user wants to view the main English version instead of the version they would get automatically redirected to based on the language setting of their browser.
So for example, user A has their browser language set to Korean, they visit the example.com site - the rewrite rules reads they are set to Korean and moves them to example.co.kr - on example.co.kr I have a button which says - Show me the English version of this website (which links to example.com) - this then links back to example.com... but currently then redirects back to example.co.kr as the rewrite script refires...
How can I code the rewrite script to allow for an overwrite rule to force it to stay on the example.com site when I want it too...?
rewrite code I have is:
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP:Accept-Language} ^en [NC]
#RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ https://example.com/ [QSA,NC,L]
Any ideas?
Cheers,
You need to modify your rules to look for a special query parameter e.g. notKO like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ / [NC,L]
Now you need to start sending ?notKO query parameter on your show me abc version of this website button click.
Clear your browser before you test this change.

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

How to redirect to folder in .htaccess?

I want to redirect all the languages except the English for example, to another folder. If you are not english and you go to www.webpage.com/work/ you will go to www.webpage.com/xx/work
Now I have this code:
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (aa|ab|af|am|ar|as|ay|az|ba|be|bg|bh|bi|bn|bo|br|co|cs|cy|da|de|dz|el|en|eo|et|eu|fa|fi|fj|fo|fr|fy|ga|gd|gl|gn|gu|ha|hi|hr|hu|hy|ia|ie|ik|in|is|it|iw|ja|ji|jw|ka|kk|kl|km|kn|ko|ks|ku|ky|la|ln|lo|lt|lv|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|na|ne|nl|no|oc|om|or|pa|pl|ps|pt|qu|rm|rn|ro|ru|rw|sa|sd|sg|sh|si|sk|sl|sm|sn|so|sq|sr|ss|st|su|sv|sw|ta|te|tg|th|ti|tk|tl|tn|to|tr|ts|tt|tw|uk|ur|uz|vi|vo|wo|xh|yo|zh|zu) [NC]
RewriteRule .* www.webpage.com/en/ [R,L]
But I want to redirect from /work/ to /xx/work/
Thanks
You need to use back reference to captured text in RewriteCond using %1:
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (aa|ab|af|am|ar|as|ay|az|ba|be|bg|bh|bi|bn|bo|br|co|cs|cy|da|de|dz|el|en|eo|et|eu|fa|fi|fj|fo|fr|fy|ga|gd|gl|gn|gu|ha|hi|hr|hu|hy|ia|ie|ik|in|is|it|iw|ja|ji|jw|ka|kk|kl|km|kn|ko|ks|ku|ky|la|ln|lo|lt|lv|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|na|ne|nl|no|oc|om|or|pa|pl|ps|pt|qu|rm|rn|ro|ru|rw|sa|sd|sg|sh|si|sk|sl|sm|sn|so|sq|sr|ss|st|su|sv|sw|ta|te|tg|th|ti|tk|tl|tn|to|tr|ts|tt|tw|uk|ur|uz|vi|vo|wo|xh|yo|zh|zu) [NC]
RewriteRule ^$ /%1/work/ [R,L]
RewriteCond %{HTTP:Accept-Language} (aa|ab|af|am|ar|as|ay|az|ba|be|bg|bh|bi|bn|bo|br|co|cs|cy|da|de|dz|el|en|eo|et|eu|fa|fi|fj|fo|fr|fy|ga|gd|gl|gn|gu|ha|hi|hr|hu|hy|ia|ie|ik|in|is|it|iw|ja|ji|jw|ka|kk|kl|km|kn|ko|ks|ku|ky|la|ln|lo|lt|lv|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|na|ne|nl|no|oc|om|or|pa|pl|ps|pt|qu|rm|rn|ro|ru|rw|sa|sd|sg|sh|si|sk|sl|sm|sn|so|sq|sr|ss|st|su|sv|sw|ta|te|tg|th|ti|tk|tl|tn|to|tr|ts|tt|tw|uk|ur|uz|vi|vo|wo|xh|yo|zh|zu) [NC]
RewriteRule ^((?![a-z]{2}/).+)$ /%1/$1/ [R,L]

.htaccess: Transparently adding a name to the request

I've read this tutorial about how to modify your .htaccess in order to server many web2py applications but it doesn't seem to work.
Here is my .htaccess
RewriteEngine On
RewriteRule ^dispatch\.fcgi/ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
RewriteCond %{HTTP_HOST} =www.moublemouble.com [NC, OR]
RewriteCond %{HTTP_HOST} =moublemouble.com [NC]
RewriteRule ^/(.*) /moublemouble/$1 [PT,L]
All I get is a 500 Internal Error and .htaccess is not my strong point.
Any clues?
It’s either the illegal space in [NC, OR] or you are getting a recursion loop since the substitution /moublemouble/… is also matched by the pattern ^/(.*). So try this:
RewriteCond %{HTTP_HOST} =www.moublemouble.com [NC,OR]
RewriteCond %{HTTP_HOST} =moublemouble.com [NC]
RewriteCond $1 !^moublemouble/
RewriteRule ^/(.*) /moublemouble/$1 [PT,L]
Or more compact:
RewriteCond %{HTTP_HOST} ^(www\.)?moublemouble\.com$ [NC]
RewriteRule !^/moublemouble/ /moublemouble%{REQUEST_URI} [PT,L]
It might be your RewriteCond causing the problem.
I haven't tried it, but you could try...
RewriteCond %{HTTP_HOST} ^www.moublemouble.com [NC, OR]
RewriteCond %{HTTP_HOST} ^moublemouble.com [NC]
RewriteRule ^/(.*) /moublemouble/$1 [L]

Resources