HTACCESS redirect if URL does not contain specific string - apache

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]

Related

htaccess turn on https and www BUT only when live, not locally

I would like to have .htaccess code that adds https and www to my URLs – but only when my site is live, not when I am developing locally on my computer.
I have the following
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
But this doesn't appear to be adding the www when my site is live. So for example https://example.com doesn't have the www and all the links are broken. For example https://example.com/about just gets a 404 Not Found error message.
Thanks for any help. I don't understand .htaccess files/code.
EDIT / UPDATE
Comparing the code to other code, should it be the following?
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Comparing the code to other code, should it be the following?
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Yes.
Your original rule explicitly removed the www subdomain. If you had requested http://www.example.com/ (note HTTP), you would have been redirected to https://example.com/ (HTTPS + non-www). But nothing would happen if requesting https://www.example.com/ - the canonical URL (the rule is skipped because the 2nd and 3rd conditions fail).
The %1 backreference contains the match from the first capturing group in the preceding CondPattern. eg. Given the CondPattern ^(?:www\.)?(.+)$ then %1 contains whatever is matched by (.+) (the first capturing group), ie. the hostname, less the optional www. prefix.
There is no difference between testing %{HTTPS} off or %{HTTPS} !on - the result is the same.
Test first with 302 (temporary) redirects to avoid potential caching issues. You will need to clear your browser cache before testing since the erroneous redirect will have been cached by the browser.
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
Checking for a port number would seem to be unnecessary here. This will fail if you are using localhost on the standard port (80 or 443) since the port number will be omitted from the Host header. Something like !^localhost would suffice, or perhaps !^localhost($|:) if you happen to using a domain name that starts localhost!

.htaccess redirect any domain to a domain apart from a specific domain, rule

I have two domains pointing at one webroot and need to force any traffic to one of those domains.
But, that means all traffic to the other domain can't get to it.
This is my current rule below
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
The second domain that I want to allow through is edit.example.co.uk
So, I've tried:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} !^edit\.example\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
But no joy, and then I've tried variations on that theme with no luck.
I think maybe conceptually, I'm struggling with the order that these get processed?
Any insights would be very gratefully received!
You should use 2 different redirect rules:
RewriteEngine On
# add https and www to example.co.uk in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.co\.uk)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# add https to edit.example.co.uk
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^edit\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Make sure to completely clear your browser cache before you test these rules.

More efficient way of redirecting multiple directives in htaccess?

I am trying to do multiple things with my .htaccess --
I am trying to redirect multiple domains to a single domain
I am trying to redirect non-www to www
I am trying to redirect non-https to https
For this example -- I am wanting to achieve https://myfinaldomain.com
So far my htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example4.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example4.com [NC,OR]
RewriteRule ^(.*)$ https://myfinaldomain.com/$1 [L,R=301,NC]
# Redirect www and http to https - non-www version of https://myfinaldomain.com
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^Home\.htm?$ / [NC,R,L]
My issues are, I can get this to work if I piece it out with ifs and such ... If I use this as-is I get too many redirects. Is there a more efficient way to look at all of this? I already combined the non-www and non-https into a single block .. I am having trouble logically combining the top section all into a single set of conditions and a single rule. Is this possible?

Can't get htaccess rule to work: force SSL on all pages, force non-SSL on two specific pages

I am no htaccess expert, but after Googling for two hours I gave up. Maybe you can help me?
I have my entire site on SSL. However, I have two pages that reference non-secure dynamic content from elsewhere. I need these to be on http instead of https.
The first part of my rules work. All the site is forced to SSL except for those two pages. However, the last part doesn't: force those two pages to non-SSL. It is probably very stupid but does anyone see where I go wrong?
#add www. if missing WORKS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
#force SSL/https WORKS
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/webshop2/localize\.php
RewriteCond %{REQUEST_URI} !^/webshop2/layoutstripper\.php
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#force http DOES NOT WORK
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You need an [OR] flag in your second SSL rule. The 2 conditions you have essentially say:
the request must be HTTPS
the URI must start with: /webshop2/localize.php
the URI must start with: /webshop2/layoutstripper.php
As you can see, the last 2 conditions will always fail, as the request can't be BOTH at the same time. If you add an [OR] flag in there, it makes it true if the URI is one or the other:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC,OR]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess RewriteCond to let requests for website.com/m/... stay untouched

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.