combine multiple conditions [AND] - .htaccess

Basically I want to join two conditions in my htaccess file.
1. either it should not be from multiple mentioned user agents and
2. it should not be from particular domain
So , suppose my user agent array is ![facebookexternalhit/ , Alexa\ Verification\ Agent ,^Pinterest/]
and domain is xyz.com
then rewrite to abc.com
I have tried this
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!(Alexa\ Verification\ Agent | bitlybot/ | Blackboard\
Safeassign | ^CCBot/ | ^Clickagy\ Intelligence\ Bot |
CloudFlare | Disqus/ | ^dproxy/ | ^EasyBib\ AutoCite |
facebookexternalhit/ | ^Twitterbot/ | ^WhatsApp/)" [AND]
RewriteCond %{HTTP_HOST} (w*)xyz.com$ [NC]
RewriteRule ^(.*)$ abc.com/$1 [R=301,L]

You may try this rule:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !"(Alexa Verification Agent|bitlybot|Blackboard Safeassign|^CCBot|^Clickagy Intelligence Bot|CloudFlare|Disqus|^dproxy|^EasyBib AutoCite|facebookexternalhit|^Twitterbot|^WhatsApp)" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L,NE]

Related

301 Redirect to remove subdomain works on and off

Here is what I want to do:
travel.xx.com => xx.com/travel
travel.xx.com/fun => xx.com/fun
travel.xx.com/page.htm => xx.com/page.htm
health.xx.com => xx.com/health
health.xx.com/food => xx.com/food
health.xx.com/page2.htm => xx.com/page2.htm
Here is my htaccess rules:
RULE SET NUM 1:
//to convert subdomain.xx.com to xx.com/subdomain and subdomain.xx.com/WHATEVER to xx.com/subdomain/WHATEVER
RewriteCond %{HTTP_HOST} ^health.xx.com$ [NC]
RewriteRule ^(.*)$ http://xx.com/health/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^travel.xx.com$ [NC]
RewriteRule ^(.*)$ http://xx.com/travel/$1 [R=301,L]
RULE SET NUM 2:
to convert xx.com/subdomain/WHATEVER to xx.com/WHATEVER
RewriteRule ^(travel|health)(\/)([a-zA-Z0-9\&^\-\_\!\:\,\.\']+)?$ $3 [R=301,L]
Rule set number one and number two work fine separately. When I put them together, rule number 2 stops working and redirects to the root.
Can anyone tell me what I am missing here or give me a solution for this?
Thanks,
Instead of 3 rules shown you just need these 2 rules doing same thing:
RewriteCond %{HTTP_HOST} ^(health|travel)\.(xx\.com)$ [NC]
RewriteRule ^/?$ http://%2/%1/ [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^(health|travel)\.(xx\.com)$ [NC]
RewriteRule ^(.+)$ http://%2/$1 [R=302,L,NE]
Make sure to keep this code before other rules.

advanced .htaccess mod_rewrite from domain to subdirectory

I've found a few threads with similar issues, but haven't had any luck modifying the answers to try and meet the requirements of my site. I have a primary domain, and re-write rules in place to redirect www domain to non-www domain, mapped like this:
primarydoamin.uk => primarydomain.uk
www.primarydomain.uk => primarydomain.uk
RewriteCond %{HTTP_HOST} ^primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$
RewriteRule ^(.*)$ primarydomain.uk/$1 [R=301,L]
I've a number of add-on / mapped domains against my hosting and I'd like to rewrite based upon a pattern:
drop 'www'
rewrite to a sub-directory of the primary domain (with periods '.' replaced to dashes '-')
only execute when the above rule, redirecting www domain to non-www domain traffic is not met, for example:
www.fishandchips.co.uk => primarydomain.uk/closed/fishandchips-co-uk
www.mushypeas.com => primarydomain.uk/closed/mushypeas-com
pukkapie.uk => primarydomain.uk/closed/pukkapie-uk
Currently I'm copying and pasting the following logic:
RewriteCond %{HTTP_HOST} ^(www.)?fishandchips.co.uk$
RewriteRule ^(.*)$ http://primarydomain.uk/closed/fishandchips-co-uk [R=301,L]
Your first set of rules can be resumed as:
RewriteCond %{HTTP_HOST} ^(www\.)?primarydomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk%{REQUEST_URI} [R=301,L]
For your second set of rules I would try something like:
# This one will work for domains with single extension like .com
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2%{REQUEST_URI} [R=302,L]
# This one will work for domains with 2 extensions like .co.uk
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2-%3%{REQUEST_URI} [R=302,L]
The %{REQUEST_URI} at the end will ensure it passes down any URL it was coming from that domain.
NOTE: Keep in mind this is an untested solution. It should work as expected, however it does not take into account multilevel sub domains into it, which you will have to adjust as you go.
Also I have set it to 302 instead of 301, use 302 until you confirm its fully working then move it to 301 and also make sure you're using a different browser or cleared your cache to make sure you're not viewing a cached redirect.

'View Full Site' add ?m=0 to all inside pages

RewriteRule ^(.*)\.php$ ^(.*)\.php$?m=0 [R=301,L]
I want all inside pages to be rewritten to ?m=0 coming from SITEDOTCOM?m=0
So basically if someone lands comes to the full site using the trailing ? i want all the links to be ?m=0
??
REASON: Mobile redirect Full Site button works once. Does not work if user clicks on something. returns to mobile site.
my code looks like this
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}]
# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|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 to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.Site.com%{REQUEST_URI} [R,L]
Tried top code return infinite loop.? thoughts?
This line here:
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
should probably be checking the query string instead as well:
RewriteCond %{QUERY_STRING} !(^|&)mobile=0(&|$)
Because the cookie isn't sent to the browser until the server responds, by that time the redirect is already made. The two CO lines creates a cookie to be sent to the browser, and immediately you redirect them to mobile before there's a response.
How about something like this? Change the redirect rule to:
RewriteCond %{HTTP:Cookie} mobile=1(;|$)
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ http://m.Site.com%{REQUEST_URI}?mobile=1 [R,L]
RewriteCond %{HTTP:Cookie} \mobile=0(;|$)
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ http://Site.com%{REQUEST_URI}?mobile=0 [R,L]

.htaccess allow if url contains a word

I want to allow access to specific domains. For example if domain contains the word asdf it should allow access. I final attempt before asking was:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*asdf.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} !^.*1234.*$
#RewriteRule .* - [F]
So here I tried to restrict access to all but domains that contain asdf or 1234.
You need to use %{HTTP_HOST} for checking the domain in URL instead of %{HTTP_REFERER}.
Can you try this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^.*(asdf|1234)\. [NC]
RewriteRule .* - [F]
Anubhava gave me a clue but not with the http_host. Finally the problem was the OR.
Now the following worked like a charm:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*(1234|asdf).* [NC]
RewriteRule .* - [F]
So HTTP_REFERER did what it should do (check the domain accessing). And the | worked as the or argument I needed.

.htaccess redirect from subdomain's articles to main domain

I have subdomains: sub1.tld.com, sub2.tld.com and so on. This subdomains contain important info. But I want to prevent opening any other urls, for example: sub1.tld.com/anypage or sub2.tld.com/dir/more/anyurl.html. This urls must be redirected to main domain tld.com: tld.com/anypage or tld.com/dir/more/anyurl.html
So, i have to check 2 things before redirection:
it's any subdomain
it's not subdomain's root page
I created this rules:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://tld.com/$1 [R=301,L]
This set of rules works as expected (redirects sub1.tld.com/anypage to tld.com/anypage) but also redirects sub1.tld.com to tld.com. Where is the error? 2nd string is wrong?
Try this:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$ [NC]
RewriteRule ^/(.+)$ http://tld.com/$1 [R=301,L]
Or you can use this string to check if it root:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /\ HTTP/
For some unknown reason the most reliable for checking variable was %{THE_REQUEST}, not %{REQUEST_URI}.
%{THE_REQUEST} contains raw browser's query to web-server: for example, GET /anypage.html HTTP/1.1, if you are opening url http://anysite.com/anypage.html.
So, here you are complete working rule-set for .htaccess:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$
RewriteCond %{THE_REQUEST} ^get\ /(.+)\ http.* [NC]
RewriteRule ^(.+) http://tld.com/$1 [R=301,L]

Resources