How to redirect to folder in .htaccess? - .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]

Related

Htaccess www to non-www but keep the url intact

I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.

How to redirect based on browser language using .htaccess

I've one italian site in the dir "www.example.com/it_IT" and one english site on "www.example.com/en_GB/"
I've tried to redirect the users based on browser locations. It works fine with index.php.
But it is not redirecting for complex urls such as "www.example.com/something/something" is not redirecting to
"www.example.com/it_IT/something/something" if the browser language is italian or "www.example.com/en_GB/something/something" if the location is UK
This is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://example.com/en_GB/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^$ http://example.com/it_IT/ [L,R=301]
Try this :
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{REQUEST_URI} !en_GB [NC]
RewriteRule ^(.*)$ http://example.com/en_GB/$1 [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteCond %{REQUEST_URI} !it_IT [NC]
RewriteRule ^(.*)$ http://example.com/it_IT/$1 [L,R=301]

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 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]

Redirect loop in 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]

Resources