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]
Related
From our client's site (http://www.cfm33.com/) we need to redirect all mobile devices to the mobile site (http://m.cfm33.com/), and this works.
But we really need an exception for the reservation page, so that users even on mobile get redirected to this reservation page of the desktop site.
Here's what's in the .htaccess :
## Mobile redirect
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone|Android|iPad|iPod|Blackberry
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.png|\.gif|\.svg)$ [NC]
RewriteCond %{REQUEST_URI} !reservation-formation.html [NC]
RewriteRule .* http://m.cfm33.com/ [R]
## End mobile redirect
Any idea why the exception does not work ? We've been trying for a few hours and still no clue...
Any idea ? Could this have something to do with some custom routing on the desktop site ?
Thanks
David
Try this rule:
## Mobile redirect
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone|Android|iPad|iPod|Blackberry
RewriteCond %{THE_REQUEST} !/reservation-formation\.html [NC]
RewriteRule !\.(jpe?g|png|gif|svg)$ http://m.cfm33.com/ [R]
## End mobile redirect
Replaced REQUEST_URI by THE_REQUEST as other rules might be affecting REQUEST_URI whereas THE_REQUEST remains unchanged.
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.
Based on what was written here, I was able to set up a mobile device detection using htaccess. I added the functionality to be redirected back to the desktop site from m.example.com in case the user does not visit the site with a mobile device. Additionally I added an environment variable to preserve http/https requests:
# Set an environment variable for http/https.
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=ps:http]
# Check if m=1 is set and set cookie 'm' equal to 1.
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
RewriteRule ^ - [CO=m:1:example.com]
# Check if m=0 is set and set cookie 'm' equal to 0.
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ - [CO=m:0:example.com]
# Cookie can't be set and read in the same request so check.
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device.
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$
# Check if we're not already on the mobile site.
RewriteCond %{HTTP_HOST} !^m\.
# Check if cookie is not set to force desktop site.
RewriteCond %{HTTP_COOKIE} !^.*m=0.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %{ENV:ps}://m.example.com%{REQUEST_URI} [R,L]
# Check if this looks like a desktop device.
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile)" [NC]
# Check if we're on the mobile site.
RewriteCond %{HTTP_HOST} ^m\.
# Check if cookie is not set to force mobile site.
RewriteCond %{HTTP_COOKIE} !^.*m=1.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %{ENV:ps}://example.com%{REQUEST_URI} [R,L]
This works fine except one thing: When I visit m.example.com/?m=1 from a desktop device, I'm redirected to example.com although I should "stay" on m.example.com. When I try again, I "stay" at m.example.com. It seems as if the cookie isn't set and/or read correctly the first time.
Any ideas what could be the problem?
Well it's kind of hard typing a title to describe what I want to do.
Basically I have a website that I've been asked to develop a mobile site for. This website has various domains (such as .co.za,.com,.za.net) but they all run from the same folder on the server (so I only have one .htaccess file).
I want to be able to redirect the traffic that goes to www.example.co.za to m.example.co.za and traffic that goes to www.example.com to m.example.com.
How would I need to modify this .htaccess file to achieve that.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte" [NC]
RewriteRule ^$ http://m.example.com/ [L,R=302]
How about:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ http://m.%2/ [L,R=302]
Is it possible to accept traffic from only one domain, ideally using a .htaccess file?
I want my site to only be accessible via a link on another site I have.
I know how to block one referring domain, but not all domains
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC]
RewriteRule .* - [F]
this is my full rewrite code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !domain\.co.uk [NC]
RewriteRule .? - [F]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I think it is working, but none of the assets are getting loaded and I get a 500 error when I click on another link.
Make that something like:
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !yourdomain\.com [NC]
RewriteCond %{HTTP_REFERER} !alloweddomain\.com [NC]
RewriteRule .? - [F]
The first RewriteCond checks that the referrer is not empty. The second checks that it doesn't contain the string yourdomain.com, and the third that it doesn't contain the string alloweddomain.com. If all of these checks pass, the RewriteRule triggers and denies the request.
(Allowing empty referrers is generally a good idea, since browsers can generate them for various reasons, such as when:
the user has bookmarked the link,
the user entered the link manually into the address bar,
the user reloaded the page,
the browser is configured not to send cross-site referrer infromation, or
a proxy between your site and the browser strips away the referrer information.)