Rewrite Cond to exclude seperate url - .htaccess

I'm using .htaccess to redirect mobiles using code found here RewriteCond to exclude a path not working
I've tried rewriting the part that excludes path to exclude a URL but can't get it to work.
Basically, i have a second url used on my hosting account which is an addon domain and is stored in a subfolder of my public html folder. My other website is stored on the public html folder with the .htaccess to redirect to a subdomain for mobile for that website.
Any suggestions on how i can stop the addon domain redirecting to the mobile version of the main website?
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
# Skip next rule if mobile=0 [OR] if it's a file [OR] if /path/
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^.*/path/.*$
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
RewriteCond %{HTTP_PROFILE} !^$ [OR]
RewriteCond %{HTTP_X_WAP_PROFILE} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !mobile=0(;|$)
# Don't redirect "path" pages
RewriteCond %{REQUEST_URI} !^.*www.addondomain.com/.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com/ [R,L,NC]

Add that below RewriteBase /:
RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.com$ [NC]
RewriteRule ^ - [L]
Indicating to do nothing for addondomain.com

Related

Htaccess allow access to directory only for mobile device

I want to allow access to a character directory and his subdirectory too, only for mobile device.
I write this condition but that not work , this is my htaccess rules:
Options +FollowSymlinks -MultiViews
##Setting rewrite base to / here.
RewriteBase /
RewriteEngine on
**RewriteCond %{HTTP_USER_AGENT} !(iphone|ipad|android) [NC]
RewriteCond %{THE_REQUEST} character [NC]
RewriteRule ^ - [F,L]**
##Rewriting for page1 uri here.
RewriteRule ^page1$ page1.php
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/FOF/news/page/page1\.php\s [NC]
RewriteRule ^ /customPage? [R=301,L]
##Internal rewrite rules here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customPage/?$ FOF/news/page/page1.php [L,NC]
Thank for help
Just block url request from any other device other than mobile to character folder/directory then it should only allow requests from mentioned devices to that folder.
Make sure to place these rules at the top of your htaccess rules file.
RewriteEngine ON
RewriteCond %{HTTP_USER_AGENT} !(iphone|ipad|android) [NC]
RewriteRule ^character - [F,L]
2nd solution: Above solution considers that your character directory stays in root directory if it can be any sub-directory in whole uri itself then try following.
RewriteEngine ON
RewriteCond %{HTTP_USER_AGENT} !(iphone|ipad|android) [NC]
RewriteCond %{THE_REQUEST} character [NC]
RewriteRule ^ - [F,L]

htaccess dynamic subdomains but redirect main domain to https-www

I am using opencart e-commerce package and have my main domain redirect to use https & www but i require some sub domains, so far in my .htaccess i have
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
This works fine and i can create new sub domains in the folder called subdomains - but if a folder is not found it shows a 500 error i have tried adding in checks for folders and files but this still returning 500 errors with the error -
Request has exceeded the limit of 10 internal redirects
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
What can i use that will only show the subdomain if the folder and file is there otherwise redirect the request back to the main domain?
So I have managed to stop the 500 errors and show a 301 moved permanently page rather then a redirect - for some reason I cannot figure out how to redirect back to the main domain if the directory doesn't exist
## redirect the main domain to www-https
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
## check the subdomain folder
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
## show the index.php file in the subdomain directory
## doesn't work but shows a 301 page ?
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*) /subdomains/index.php [R=301]

How can I create virtual subdomain which points to a folder?

I am trying to create a website that would create its portfolio, currently once created your page that has the URL domain.com/content/username but I would like to create a subdomain for each user therefore username.domain.com. Do you have any idea how to do this? I tried this:
Options +FollowSymLinks -Multiviews
RewriteEngine on
RewriteBase /
#
# Canonicalize the hostname
RewriteCond www.%{HTTP_HOST} ^(www)\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(mine-app\.fr). [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(mine-app\.fr):[0-9]+
RewriteRule (.*) http://%1.%2/$1 [R=301,L]
#
# If subdomain is NOT www
RewriteCond %{HTTP_HOST} !^www\.mine-app\.fr [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.mine-app\.fr$
# Rewrite if requested URL resolves to existing file or subdirectory in /content/<content>/ path
RewriteCond %{DOCUMENT_ROOT}/content/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/content/%1/$1 -d
RewriteRule (.*) /content/%1/$1 [L]
But nothing worked. How can I solve this?

.htaccess: Allow specific URL

I have an angularJS site and the .htaccess file have been configured to redirect all traffic from mysite.se/* to mysite.se/index.html.
It also configured with prerender.io to let search engines to access the site.
Question: I want to add a blog to blog.mysite.se, and the problem is of course that it will be redirected to mysite.se/index.html. What do I need to add to the .htaccess file to let through the blog.mysite.se?
Thanks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^MYSITE\.se$ [NC]
RewriteRule ^.*$ http://www.MYSITE.se%{REQUEST_URI} [R=301,L]
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "kEJ0CC1gMnj6V0J4u8xu"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(.*)$ http://service.prerender.io/http://%{HTTP_HOST}$1 [P,L]
# If non existent
# Accept everything on index.html
RewriteRule ^ /index.html
Insert this rule just below RewriteEngine On line to skip blog subdomain from current rule:
RewriteEngine On
RewriteCond %{HTTP_HOST ^blog\. [NC]
RewriteRule ^ - [L]
# rest of rules here

What lines of code would I enter in the .htaccess perform the redirect only if the target is the homepage?

What lines of code would I enter in the .htaccess perform the redirect only if the target is the homepage? That is the main question here. I'll elaborate:
I used the method described at: Mobile Redirect using htaccess to redirect mobile. Works great. Now, what my client is trying to do is when someone is on mobile, if they are trying to access a page that is not the homepage (i.e. a blog), then they do NOT get redirected. So, if the target is the homepage, the redirect works as normal. Make sense?
So, I'll simplify it again -- what lines of code would I enter in the .htaccess perform the redirect only if the target is the homepage?
Here is what I have (minus the detection part)
RewriteCond %{REQUEST_URI} ^/index.php$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
#there is a bunch of other user agents #
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.thesiteurl.com [R,L]
These first two lines should work , replace RewriteRule as per your need .
RewriteCond %{REQUEST_URI} ^/index.php$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /something.php
Yes It should work like :
RewriteCond %{REQUEST_URI} ^/index.php$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$
RewriteCond %{HTTP_HOST} !^m\.
RewriteCond %{HTTP:Cookie} !\smredir=0(;|$)
RewriteRule ^ m.example.org%{REQUEST_URI} [R,L]

Resources