From a phone, if the user tries to access website.com, they are redirected to mobile.website.com. However, mobile.website.com makes AJAX requests to website.com, so I make all requests go through website.com/m/.... This isn't working:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/m/ [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
Specifically the line:
RewriteCond %{REQUEST_URI} !^/m/ [NC]
It should cancel the rewrite rule if the url matches website.com/m/.... Any ideas?
Thanks!
Change your code with this:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} (android|blackberry|ipad|iphone|ipod|iemobile|opera [NC]mobile|palmos|webos|googlebot-mobile) [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com$ [NC]
RewriteRule ^(?!m/).*$ http://www.mobile.website.com%{REQUEST_URI} [L,R,NC]
Also if this still doesn't work then please post matching lines from your Apache's access.log and error.log.
Related
I have looked through various other posts on here but I can't seem to find any that show how to do a redirect in HTACCESS if the url does NOT contain a specific string. What I need to do is force HTTPS/WWW to all requests unless the request is for appfeed.domain.com. In that case, I don't want to redirect at all but just let the request go through.
Here is what I have now.
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
This works, but it also redirects my appfeed.domain.com requests to www.appfeed.domain.com, which obviously breaks the request. can anyone help me out here?
Could you please try following, written as per your shown samples. This will process requests only for for domain which is not appfeed.domain.com. Adding 1st condition as per of that and rest of the conditions from OP's htaccess itself.
RewriteEngine ON
RewriteCond %{HTTP_HOST} !appfeed\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I want to accomplish the following:
redirect non-www to www for all users
redirect desktop users to www.example.com/homepage
redirect mobile users to www.example.com/m
hide /homepage and /m from url so users will see www.example.com only
Here is my htaccess code. I am having a lot of problems with it, like things not redirecting to /m, and iphone users see "too many redirects", and sometimes desktop users are taken to /m even though they should be taken to /homepage.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /homepage/ [R=301]
</IfModule>
RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPod.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iemobile.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*blackberry.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://www.example.com/m [R=301]
This will give you too many redirects because you're only looking at the user agent, not the URI that's being requested, so every time a mobile user agent matches you continually redirect them to www.example.com/m. You can also add all the user agents into a single regex to reduce the number of rules you need to maintain.
You need to add a condition to look at the REQUEST_URI. Something like to the following should do:
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://www.example.com/m [R=301,L]
I have the following problem. I have a site where some pages must be redirected to the https-secured equivalent of the link.
I tried several things but in the end it seems that the server ends up in a loop redirecting :(
Now I redirect every page to the https page but that is not what I want to.
This is the code I use right now:
RewriteCond %{http_host} ^www.domain.nl
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.nl/$1 [L,R=301]
I would like to have al urls starting with bo- have https.
examples:
http://www.domain.nl/bo-users -> redirects to https://www.domain.nl/bo-users
http://www.domain.nl/bo-groups -> redirects to https://www.domain.nl/bo-groups
but
http://www.domain.nl/about-us -> stays at http://www.domain.nl/about-us
It is clear to me I need some wildcards in the rewrite condition for all urls startin with bo-.
We are using apache on the server.
Hope someone can send me in the right direction.
Thanks,
Frank
Update after the tips from Anubhava.
I current;y have this htaccess but I can't get it working (even when clearing the cache in my browser)
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
#rewrites een url to www.
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[a-z]{2,6}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Hope for some extra help!
Thanks,
Frank
I just opened another call. The initial answer worked fine!
Use this rule:
# force HTTPS for bo- URIs
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
# force HTTP for non bo- URIs
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
I'm dealing with a site that supports both English and Spanish, and I'd like to know how to redirect all the requests to the language-specific subdomains to the main domain (en.mysite.com and es.mysite.com to mysite.com).
The whole site is programmed in PHP and it has a main script index.php that processes the language and section GET parameters and displays stuff accordingly.
Now, I tried to do the following in the .htaccess file in the root of mysite.com. I think it clarifies what I'm trying to do:
RewriteEngine On
# English redirects
RewriteRule ^en.mysite.com$ index.php?language=English§ion=Main
RewriteRule ^en.mysite.com/store$ index.php?language=English§ion=Store
RewriteRule ^en.mysite.com/create_account$ index.php?language=English§ion=CreateAcc
# Spanish redirects
RewriteRule ^es.mysite.com$ index.php?language=Spanish§ion=Feed
RewriteRule ^es.mysite.com/comprar$ index.php?language=Spanish§ion=Store
RewriteRule ^es.mysite.com/crear_cuenta$ index.php?language=Spanish§ion=CreateAcc
But this doesn't work. Can anyone tell me what I'm doing wrong here?
You can't include the hostname as part of the match in a RewriteRule, as it only matches against the URI. You'll need to use a separate RewriteCond that matches against the hostname for each of your rules:
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English§ion=Main
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^store$ /index.php?language=English§ion=Store
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^create_account$ /index.php?language=English§ion=CreateAcc
Then the spanish rewrites:
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English§ion=Feed
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^comprar$ /index.php?language=English§ion=Store
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^crear_cuenta$ /index.php?language=English§ion=CreateAcc
I use the following snippet in my htaccess:
# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
However, inside my mobile app, I make AJAX requests to www.website.com/mobile, and they are all failing. How can I send these AJAX requests and still redirect them?
You can use:
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]
It will unblock the /mobile and your ajax requests will work.