Internal htaccess redirect vs direct navigation - .htaccess

I have an .htaccess redirect script for my mobile detection:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|android|nokia|BlackBerry) [NC]
RewriteCond $1 !^mobile/ [NC]
RewriteRule ^(.*)$ http://www.example.com/mobile/ [R=301,L]
When I get redirected to the mobile site, my links don't work that point to:
www.example.com/img0.jpg
If I navigate directly to the site by browsing to:
www.example.com/mobile/
the links work perfectly.
What am I missing in the .htaccess file that is affecting this linking?
Thanks.
EDIT:
I used your script and then added this into the mobile directory, not exactly sure why it works, but it does!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mobile\.example\.com$
RewriteCond %{REQUEST_URI} example.com/mobile
RewriteRule ^(.*)$ /mobile/$1 [L]

Try this:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|android|nokia|BlackBerry) [NC]
RewriteCond $1 !^mobile/$1 [NC]
RewriteRule ^(.*)$ http://www.example.com/mobile/$1 [R=301,L]

Related

.htaccess redirect to subfolder, non-www to www and https

So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]

redirect using htaccess all media in my site into mobile version

How to redirect using htaccess when mobile user acces on my jpg,bmp,gif files on my site
example : i want to redirect mysite.com/media/upload/files.jpg|jpeg|bmp|gif|png into m.mysite.com/media/upload/files.jpg|jpeg|bmp|gif|png
Here is my htaccess
RewriteEngine On
RewriteRule \.(jpg|png|gif|jpeg|bmp)$ - [L]
RewriteCond %{HTTP_USER_AGENT} (mobile|android|blackberry|brew|cldc|docomo|htc|j2me|micromax|lg|midp|mot|motorola|netfront|nokia|obigo|openweb|opera.mini|palm|psp|samsung|sanyo|sch|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windows.ce) [NC]
RewriteRule ^(.*)$ m.mysite.com [R=302,L]
the problems is, when mobile user straight accessing http:// mysite.com/media/upload/files.jpg it still not redirect into m.mysite.com/media/upload/files.jpg
Assuming that you're checking against the correct user agents, try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^m\.mysite\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} (mobile|android|blackberry|brew|cldc|docomo|htc|j2me|micromax|lg|midp|mot|motorola|netfront|nokia|obigo|openweb|opera.mini|palm|psp|samsung|sanyo|sch|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windows.ce) [NC]
RewriteCond $1 \.(jpg|png|gif|jpeg|bmp)$ [NC]
RewriteRule ^(.*)$ http://m.mysite.com/$1 [R=302,L]

no redirect rule for mobile redirect in htaccess

I use .htaccess code for mobile redirects:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|epoc|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mmp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|treo|up.browser|up.link|vodafone|wap|wap1|wap2|webos|windowsce|xda|xiino) [NC]
RewriteRule ^(.*)$ some site [R=302,L]
However, I want some mobile IPs/mobile User Agents to be excluded from the above redirection, not to be denied. I want they to see the desktop site.
How I can do this with .htaccess code?
Thank you.
You can add another RewriteCond for exclusion:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(11\.22\.33\.44|55\.66\.77\.88)$
RewriteCond %{HTTP_USER_AGENT} !(excluded-agent1|excluded-agent2|excluded-agent3)
RewriteCond %{HTTP_USER_AGENT} (mobile|android|iphone|ipod|ipad|avantgo|blackberry|blazer|compal|elaine|epoc|fennec|hiptop|iemobile|iris|kindle|sgh|brew|htc|j2me|lg|midp|mmp|mot|netfront|nokia|obigo|openweb|operamini|palm|plucker|pocket|psp|samsung|sanyo|sch|smartphone|sonyericsson|symbian|symbos|teleca|treo|up.browser|up.link|vodafone|wap|wap1|wap2|webos|windowsce|xda|xiino) [NC]
RewriteRule ^ /desktop-site [R=302,L]

.htaccess makes subdirectory appear in URL

I've been searching for this but I'm really newbie with .htacces (I find it a hell) and I cannot get the way to solve this.
I have the root folder in my server, redirecting the default domain URL to the folder "web". I have another folder, let's say subfold, and inside this, I have another folder: mobile.
What I want is that if someone types www.adomain.com it goes to root/subfold, without showing the subfold. I've done it this way:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteRule (.*)$ /subfold/$1 [L]
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteRule (.*)$ /web/$1 [L]
However, when I go to www.adomain.com/mobile it shows in the URL www.adomain.com/subfold/mobile/
My aim is that if I go to the mobile subfolder, in the URL it should show www.adomain.com/mobile
Obviously, I would like not to have to write a redirect everytime I add a subfolder, it it was possible.
How should I do that?
Thanks for your time and help!
You're getting redirected because your accessing a directory without a trailing slash. The mod_dir module, on by default, will redirect the browser and add a trailing slash if someone tries to access a directory without a trailing slash. This is because there is a information disclosure issue with indexing directories. You can either turn this off:
DirectorySlash Off
and risk having the contents of your directories get listed, even if there's an index.html file. Or add a bunch of rules to ensure there's a trailing slash:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
# redirect for trailing slash:
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteCond %{DOCUMENT_ROOT}/subfold%{REQUEST_URI} -d
RewriteRule (.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteRule (.*)$ /subfold/$1 [L]
# redirect for trailing slash
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteCond %{DOCUMENT_ROOT}/web%{REQUEST_URI} -d
RewriteRule (.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteRule (.*)$ /web/$1 [L]

Htaccess URL masking to specific folder

I have the following problem.
I need when people access to www.mydomain.com they really access to www.mydomain.com/folder, and they keep seeing www.mydomain.com in the URL.
How can this be done with de .htaccess of my server?
Thanks
create a .htaccess file in the root of the site with the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com/$ [NC]
RewriteCond %{REQUEST_URI} !^../folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-_.\s]+)?$ ../folder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?([a-zA-Z0-9/-_.\s]+)$ ../folder/$2 [L]
that's all you need.
RewriteEngine On
RewriteRule ^(.*)$ /folder/$1 [L]
and try to search before you ask something! there are lots of examples like that.

Resources