I've adapted a htaccess file that was provided here Mobile Redirect using htaccess however, what I'm trying to do is to check the user is not already in the subfolder(rather than sub domain) domain.co.uk/mobile and if not, redirect them to the mobile site.
However, this just drops into an infinite loop and therefore fails.
Can anyone tell me what the problem is and how I do this kind of RewriteCond?
RewriteEngine on
RewriteBase /
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.domain.co.uk]
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
# Check if we're not already on the mobile site
RewriteCond %{REQUEST_URI} !^/mobile/ [NC]
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://www.domain.co.uk/mobile [R,L]
You should change your mobile site check from !^/mobile/ to !^[/]?mobile. Apache does not always include the leading slash in the REQUEST_URI. This regex makes that leading slash optional, so the rule will work whether the slash is present or not.
Related
Hey everyone im trying to use Htaccess to redirect all pages to https:// while keeping the query string without fbclid if it's there.
IE.
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
They should return https://example.com/?sdfuh
Ok so after some help Ive got to this
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
This works with:
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
Not with
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
I need to figure out how to make it shorter and work with all the possibilities
Rather than literally combining the rules, I believe the main piece of knowledge you are missing out on is the use of [OR] - which means a RewriteRule won't fail if only one of the RewriteCond results in true.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R=301,L]
You can test this out here: https://htaccess.madewithlove.be?share=f4381086-80b4-52b6-883e-8921f5de2316
After alot of help this solved my issue of stripping facebooks tracker, keeping my own query string and confirming https:// is used.
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R,N]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/$2 [R,L]
I have installed an SSL on my website and I have a .htaccess file that has some conditions already written. I have looked at a number of methods shown online on how to force https. However, none of them seem to work. I keep having broken links.
The code I am currently using, the rule commented out does not work for https
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
If you would like to force HTTPS you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
I have a hosting that has a master domain and also 5 more (Addon) domains under this same host.
All the websites run Wordpress on them.
I would like to redirect the master domain to one of the addon domains and i have a problem with parts of the redirects, all the pages are redirecting to the right places but some resources seem to enter a infinite redirection loop (wp-include css for example).
the redirection iv'e tried:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^crossword\-clue\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^crossword\-clues\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.wisely\-chosen\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^wisely\-chosen\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.info\-beast\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^info\-beast\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.crossword\-clues\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.quickinfo\.co\.il$ [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.israwords\.co\.il$ [NC,OR]
RewriteCond %{HTTP_HOST} !^israwords\.co\.il$ [NC,OR]
RewriteCond %{HTTP_HOST} ^israwords\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.israwords\.com$ [NC]
RewriteRule ^(.*)$ http://www.israwords.co.il/$1 [R=301,L]
To redirect
www.israwords.com
to
www.israwords.co.il
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?israwords\.com$
RewriteRule ^ http://www.israwords.co.il%{REQUEST_URI} [L,R=301,NE]
Im trying to make a simple html web page in 3 languages. Each language has its directory, so the index.html in "/" is in english, the spanish index in "/es" and the french index is in "/fr". Im trying to create a redirection rule for each language based on browser language or a cookie establised in the href onclick by javascript. This is my .htaccess code:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [NC]
RewriteRule .* http://myweb.com/es/$1 [R=301,L]
RewriteCond %{HTTP_COOKIE} !^.*lang=FR.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (fr) [NC]
RewriteRule .* http://myweb.com/fr/$1 [R=301,L]
RewriteRule .* http://myweb.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
It is not working because a infinite loop.
Thank you very much.
Redirect loop is happening due to unconditional last rule RewriteRule .* http://myweb.com/$1 [R=301,L]. Your first 2 rules also have have an error where they are not capturing any group but using them as $1.
Have your rules this way:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [NC]
RewriteRule ^((?!es/).*)$ http://myweb.com/es/$1 [R=301,L,NE]
RewriteCond %{HTTP_COOKIE} !^.*lang=FR.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (fr) [NC]
RewriteRule ^((?!fr/).*)$ http://myweb.com/fr/$1 [R=301,L,NE]
RewriteRule ^((?!(?:es|fr)/).*)$ http://myweb.com/$1 [R=301,L,NE,NC]
What happens is that each time a user enters a page they get redirected.
e.g.: someone has a cookie that says "es" but they go to the es directory, you will still redirect them to the es page, over and over again.
You should ad a condition saying on what page each redirect should work.
something like RewriteCond %{REQUEST_URI}
edit:
Here goes:
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [AND]
RewriteCond %{REQUEST_URI} !^myweb.com/fr.*$
RewriteRule .* myweb.com/es/$1
I am using the following code in my htaccess file to detect mobile devices and redirect them to a mobile site.
But the problem its redirecting to a mobile site on Tablets and IPad too. How can I modify the code so it loads full site on Tablets and IPad.
# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.mysite.com]
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://www.mysite.com/mobi [R,L]
Thanks and appreciate it