htaccess mobile redirect issues - php

How can I redirect mobile users to /mobile/ directory?
This code work for me only if users start navigate from main path (http://www.mywebsite.it) but if they click on a link on facebook (http://www.mywebsite.it/news/title-news/123.html) they reach the non-mobile version...
RewriteCond %{HTTP_USER_AGENT} blablabla [NC]
RewriteRule ^$ http://www.mywebsite.it/mobile [R=302,L]
If I change the second line width this one:
RewriteRule ^ http://www.mywebsite.it/mobile [R=302,L]
the redirection is ok but the mobile version of website is inaccessible (images not displaying, jquery mobile doesn't work...)
Who can help me?
----|----
And if I want to exclude a subfolder? My back-end is responsive but now it is impossible to access at http://www.mywebsite.it/admin because .htaccess redirect to mobile version!

You have to exclude your existent files from the rule :
#--exclude existent files--#
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC] RewriteCond %{HTTP_USER_AGENT} blablabla [NC]
RewriteRule ^(.*)$ http://www.mywebsite.it/mobile/$1 [R=302,L]
Otherwise the Rule redirects them to the mobile directory.

Related

Redirect domain.com/shop to www.domain.com/shop

Note: Before you mark it duplicate, please read the complete question.
I already applied bellow suggestion which didn't work for me.
1 - mod rewrite from .domain to www.domain htaccess
2 - .htaccess redirect from http://www.domain/ to https://domain.com
3 - .htaccess redirect any domain/directory to www.domain/directory
We've got a shopping site which we're hosting on a host (CloudLogin).
The main site is built on Joomla CMS and Magento as shopping cart.
The main site URL is www.domain.com and shopping cart URL is domain.com/shop (Without www).
What we need is to show both URLs with (www).
So we need the Magento folder ie domain.com/shop also to be on www URL.
I found some solutions on stack overflow but they didn't work for me as it always show a message browser (Too many redirection or /page isn't redirecting properly)
Note: The .htaccess file is located in (shop) folder.
Bellow is what I tried so far:
1 -
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
The above code removes the (shop) directory from URL which turns into error.
ex: domain.com/shop/cat1/prod1 turns into www.domain.com/cat1/prod1
2 -
RewriteCond %{REQUEST_URI} !^/shop/?.*$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Above code not even apply www on it.
3 -
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Above code redirects from domain.com/shop/cat1/prod1 to www.domain.com/shop with error message (redirected you too many times.)
Which seems infinite loop on page.
4 -
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/shop/$1 [L,R=301]
This does the same thing as 3.
5 -
#RewriteCond ${HTTP_HOST} doamin.com [OR]
#RewriteCond ${HTTP_HOST} www.doamin.com
#RewriteRule ^(.*)$ http://www.doamin.com/shop/$1 [QSA,R=301,L]
Does anyone know how I can always force the correct use of .htaccess for particular URLs?
I've had a look around SO but couldn't find a suitable answer to this.
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/shop
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ http://%1/shop%{REQUEST_URI} [NE,L,R]
Did you try the following?
# anything.com to www.anything.com
RewriteCond %{HTTP_HOST} ^[^.]+(\.[^.]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/shop/$1 [L,R=301,NE]
Since requests originate from the /shop directory they will not contain that directory in the URL requested.

htaccess RewriteRule for subdomain redirection by language

I have a website in English http://example.com
Then I want to add code for a redirect to my subdomain PT language http://pt.example.com if the browser language is PT
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (pt) [NC]
RewriteRule .* http://pt.example.com [R,L]
When I add this I get a redirect loop in the browser.
I have tried example.com/pt and get the same error from the browser.
Add a RewriteCond :
RewriteCond %{HTTP_HOST} !^pt\.example\.com

Mobile Redirect with pretty URLs in .htaccess

I am trying to redirect my main site (www.mainsite.com) to my mobile site (m.mainsite.com) based on user agent in .htacess. I used the following code and it redirects to the m.mainsite.com properly.
RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\ CE|Windows\ Mobile|Windows\ Phone|nokia|UP.Link|UP.Browser)
RewriteRule (.*) http://m.mainsite.com/$1
However, when taken to the mobile site I seem to be losing the rewritten URL. So what should be m.mainsite.com/page1 goes to something like m.mainsite.com/index.php?url=page1.
I tried rewriting the URL using .htaccess on the mobile site but could not figure out how to remove the part after index.php. Can anyone help me figure out how to remove "index.php?url=" from m.mainsite.com/index.php?url=page1?
Or would it be easier to figure out how to address this via htaccess in the non-mobile version during the redirect.
Full code looks like:
[code]
RewriteCond %{HTTP_HOST} ^mainsite.com
RewriteRule (.*) http://www.mainsite.com/$1 [R=301,L]
RewriteRule ^(.*)\_(\?.*)?$ $1$2 [R=301,L]
RewriteRule .* - [env=REWRITE_ON:1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|gif)$
RewriteRule ^(.*)$ index.php?url=$1 [L]
RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\ CE|Windows\ Mobile|Windows\ Phone|nokia|UP.Link|UP.Browser)
RewriteRule (.*) http://m.mainsite.com/$1[code]
Thanks
Gary
Looks like you have other rules that's interferring. You need to put all of your redirect rules before any internal routing rules. The index.php?url=page1 looks like your routing rule.
Place the mobile redirect near the top of your htaccess file.

Need helping on htaccess redirect [duplicate]

Hello Folks at Stackoverflow,
I'm looking to redirect desktop users away from mobile site with .htaccess:
I already have this code which works perfectly when a mobile user tries to access the mobile version of my website example: m.website.com.
RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://m.website.com [L,R=302]
However if a user types m.website.com on a desktop browser, it goes directly to the mobile version content.
Is there a way to add any additional code to the .htaccess file to make it work in a way that when a desktop user attempts to go to m.website.com it remains in the desktop version.
Place this additional rule in .htaccess of DOCUMENT_ROOT of m.website.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera\smobile|palmos|webos) [NC]
RewriteRule ^ http://website.com%{REQUEST_URI} [L,R=302]

Redirect using htaccess based on referrer

We only want users from a specific website to use our services. Is there a way to redirect all traffic that does not come from a specific referrer, to a website of our choosing, via htaccess?
Also, this is for the first page only. So if they get to our site, they're going to browse a new page, and their referrer for the new page would apparently be the site they are already on.
Thank you!
Try adding this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://the-ok-domain.com [NC]
RewriteRule ^/?first-page.html$ http://the-website-of-your-choosing.com/ [L,R]
You could also make it so you add your own domain to the referer check:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://the-ok-domain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://your-domain.com [NC]
RewriteRule ^ http://the-website-of-your-choosing.com/ [L,R]
Then you can include all of your pages in the check.
Note that referers can be easily forged and any htaccess file using mod_rewrite in any of your subdirectories will supercede these rules (unless those htaccess files have the RewriteOptions inheret option set)
Didn't work for me, I've made this small change to redirect traffic from google:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^(.*)\.google\.(.*) [NC]
RewriteRule ^(.*)$ https://www.my-site.it/$1 [L,R]

Categories