HTACCESS redirect if URL does not contain specific string

Viewed 592

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?

1 Answers

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