I am currently using this in my .htaccess:
RewriteCond %{HTTP_USER_AGENT} Chrome
RewriteRule .* - [L]
RewriteRule .* URL [R,L]
It work in Windows and Android . But not in Ios . I try
RewriteCond %{HTTP_USER_AGENT} Chrome|Crios
Still won't work. Anyone please help me .
As mentioned in the official documentation of Google chrome browser link, it uses
either
Chrome or CriOS (for iOS)
in user agent header. So you need to check for both in case you wanted to test for chrome browser
The rule for it can be following
RewriteCond %{HTTP_USER_AGENT} ^.*(Chrome|CriOS).*$
RewriteRule .* - [L]
RewriteRule .* URL [R,L]
The regex is working and can be verified at regex tester
However there is no single approach through which you can be sure that the browser is official Chrome because many other browsers are also using chrome in their user agent header to ride the popularity of Google Chrome like
Dragon,Edge,Flock,Iron,Kinza,Maxthon (to name a few)
You can go to http://www.useragentstring.com/ to find the user agents of other browsers that uses chrome in their user agent header and exclude them in the rewrite rule.
So the final rewrite rule can be following
RewriteCond %{HTTP_USER_AGENT} ^.*(Chrome|CriOS).*$
RewriteCond %{HTTP_USER_AGENT} !^.*(Aviator|ChromePlus|coc_|Dragon|Edge|Flock|Iron|Kinza|Maxthon|MxNitro|Nichrome|OPR|Perk|Rockmelt|Seznam|Sleipnir|Spark|UBrowser|Vivaldi|WebExplorer|YaBrowser).*$
RewriteRule .* - [L]
RewriteRule .* URL [R,L]
The browser list is from link
Related
There is website with two different pages with photos:
site.com/photo1.php and site.com/photo2.php; both pages are redirected by htaccess in Search Engine Friendly URLs, so site.com/photo.php?date=180322&name=event transforms to site.com/photo/180322/event/
there are two snippets from htaccess, but the first one doesn't work in firefox. In chrome works good
# photo1.php - redirect (1st - NOT workable in FF)
RewriteRule ^photo1/([0-9]{6})/([a-z]+)/?$ /photo1.php?date=$1&name=$2&norec [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} date=([0-9]{6})&name=([a-z]+)$
RewriteRule ^photo1.php$ /photo1/%1/%2/? [L,R=301]
# photo2.php - redirect (2st - workable in FF)
RewriteRule ^photo2/([0-9]{6})/([a-z]+)/?$ /photo2.php?date=$1&name=$2&norec [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} date=([0-9]{6})&name=([a-z]+)$
RewriteRule ^photo2.php$ /photo2/%1/%2/? [L,R=301]
snippets are identical, but firefox at the 1st variant redirects to site.com/photo1/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/
How should I solve this problem with FF?
I have solved the problem. I had to switch off Cashing in Preferences-Network. Now redirecting works as I wanted.
This question may look like its been asked already and i have seen them all, i've been looking and attempting a lot of answers AND answers that weren't approved. I have successfully made it so that if the user goes to desktop version it will go to the mobile site and even if they go to places such as.
www.domain.com/aboutus
it would take them to
m.domain.com/?page=aboutus
So here is where the problem lies, not that it doesn't work, but I've been trying to remove the $_GET variable from the redirection the "?page=" part.
my .htaccess looks something like...
<IfModule mod_rewrite.c>
RewriteEngine on
# if it is not a file or folder, rewrite to index.php?page=<value>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^index.php(.*)$ http://m.domain.com/ [QSA,L]
</IfModule>
I've tried adding the request filename with the redirection for mobile but to no avail. There are websites who have achieved it like 9gag by using the in built Google Chrome inspect element, google changes the user agent to devices that are selected (Mobile Phones) and I've used that to test how the redirection goes. so if i write 9gag.com/hot - it would take me to m.9gag.com/hot not m.9gag.com/?page=hot or wherever.
Thanks in advance, I've really been bothered by this.
You need to check the mobile redirect first, and you need to include the request URI.
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect mobile requests to the mobile site
# (but don't redirect when accessing certain directories)
RewriteCond %{REQUEST_URI} !^/images [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^(.*)$ http://m.domain.com/$1 [R=302,L]
# If it is not a file or folder, rewrite to index.php?page=<value>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
You can make the redirect permanent by changing 302 to 301.
Let me explain the problem. We have a website in 2 languages: fr & nl (dutch).
When you arrive on our website, you land on www.domain.be which redirects
you (as you can see in the following code) to the dutch version if your browser language
is set to 'nl' and if it's set to 'en' (because dutch people often use this language for they browser) or leave you on www.domain.be / fr.domain.be (both url are work to call the website - the fr... one is more in response of the nl... one)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
On the website, you can choose, by clicking on 2 links (in the top right corner), if you want to go to fr.domain.be or nl.domain.be.
When you click on one of those link, the htaccess redirects you even if you want to go to the fr part (while navigating on the dutch one) and the same on the nl part.
How can I solve that? I would like the htaccess to only redirect you when you first come to the website but then be desactivated and allow the user to choose his language if he wants to.
Could you please help me? I'm on this for like two days...
You're only redirecting the site base /, so swith directly to another page should not be a problem.
Can't you just use a fake index page when you manually switch ? Like http://nl.domain.be/index
You can create multiple conditions for a rule set so you could add an aditional check that the redirect only happens when you do not match the HTTP_REFERER to your domains.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
the ! means NOT so basically you are saying when the HTTP_REFERER does NOT match the url pattern, which is "(wildcard).domain.be".
This will prevent the rule from being run if they are on your site currently and trying to change the language.
NOTE: I'm not near an apache box to test this so my syntax might be off but that should get you down the right path.
I've been researching my question, rewording it in many ways but I still can't seem to find an answer. It could also be that it can't be done, hopefully someone can help me. I'm pretty new to .htaccess files but I've been creating a mobile website for my company's corporate site.
What I want it to do is when you go to the mobile site, it will give you an option to see our full corporate site or the mobile site. I have it redirecting to that page just fine, but when you chose to see the full site, it will redirect you back to the same page, instead of seeing the full site. I have the mobile site in a subdirectory "www.companysite.com/mobile". Can I have it so that when you choose the option to view the full site it won't keep redirecting you to the mobile site? Here is my code right now:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} android
RewriteRule .* http://companysite.com/mobile [R=301,L]
RewriteCond %{HTTP_USER_AGENT} BlackBerry
RewriteRule .* http://companysite.com/mobile [R=301,L]
Thank you in advance!
Some people like using cookies but it's a little more tricky. A sample solution would be:
## Setting a cookie and variable for mobile users
RewriteRule .* - [E=IsMobile:false]
RewriteRule .* - [E=MobileCookie:false]
# Mobile Redirection
# Blackberry
#RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "blackberry" [NC]
RewriteRule ^$ http://mobile.website/ [L,R=302]
# End Blackberry
# Mobile Redirection for IPhone
RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile|iemobile|iphone|ipod|opera mobile" [NC]
RewriteRule ^$ http://mobile.website/ [L,R=302]
# End other IPhone
# Mobile Redirection for other Mobile
RewriteCond %{HTTP_USER_AGENT} "android|palmos|webos" [NC]
RewriteRule ^$ http://mobile.website/ [L,R=302]
# Set isMobile variable to true
RewriteRule .* - [E=IsMobile:true]
# End other Mobile
## Assume the mobile cookie is not set:
RewriteRule .* - [E=MobileCookie:false]
## Checking for mobile cookie
RewriteCond %{HTTP_COOKIE} gpf_mobileoff=true
RewriteRule .* - [E=MobileCookie:true]
## Combining IsMobile & MobileCookie to make one rule
RewriteRule .* - [E=ShowMobile:false]
RewriteCond %{ENV:IsMobile} ^true$
RewriteCond %{ENV:MobileCookie} !^true$
RewriteRule .* - [E=ShowMobile:true]
########## End - Custom redirects
Of course you would need to change mobile.website in the code above and use js or some other method to inject a mobilecookie.
Personally, I like the HTTP_REFERRER condition more.
I think you can also check the referrer. If the referrer is your own site you don't do the redirection. Something like
RewriteCond %{HTTP_USER_AGENT} android
RewriteCond %{HTTP_REFERER} !yoursite\.com
RewriteRule .* http://companysite.com/mobile [R=301,L]
edit:
I should give the complete solution:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (android|BlackBerry)
RewriteCond %{HTTP_REFERER} !yoursite\.com
RewriteRule .* http://companysite.com/mobile [R=301,L]
I have restricted hotlinking my files in using htaccess. But I need those files to be displayed in Google Doc Viewer.
In htaccess I allow Google Docs to hotlink my files, but it is not working. Please help me in this
Below is the code I used in my htaccess file
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomainname.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?docs.google.com [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png|jpg|doc|ppt|xls|pdf|html|htm|xlsx|docx|pptx|swf) [NC]
RewriteRule .*\.(gif|png|jpg|doc|ppt|xls|pdf|html|htm|xlsx|docx|pptx|swf)$ http://mydomainname.com/ [NC]
Using the referrer from https urls will probably not be very successful.
Note also that the remote in the googledocs are not fetched by the browser from your server, but from a server process at google.
I had the same problem and the trick I found was to allow a specific User-Agent:
RewriteCond %{HTTP_USER_AGENT} !(.*Feedfetcher-Google.*)
Of course it is easily spoofable, but in "normal usage" your hotlink protection will still work.
Oliver S is correct, but maybe the name of the user agent is changed now..
I tried
RewriteCond %{HTTP_USER_AGENT} !(.*Google.*)
And it worked perfectly