JimmyBorofan
JimmyBorofan

Reputation: 157

Ignore specific file in htaccess when redirecting to https

ive tried many combinations and looked through this site for examples but none I try seem to work,

I have an email image stored on my clients site, by default ALL pages are redirected through https, but this causes an issue when sending out html emails with the image stored on the server, I have a section that allows me to ignore given folders so that they are not pushed through index.php giving me an area I can test and do other things, but even these are sent through https,

Is there a way to either ignore a single file or a directory in htaccess that prevents it from being sent via https?

This is where I am at, at the moment and I have spent an hour trying to resolve this so please any help is appreciated

RewriteEngine on
#This is the directory I wish to be ignored:
RewriteCond %{REQUEST_URI} !^(.*/)?email/image/\.png$ [NC]
#Dont send these requests to index.php:
RewriteRule ^(ajax|test)($|/) - [L]
RewriteBase /
RewriteRule ^m/([^/\.]+)/?$ index.php?module=$1 
RewriteRule ^a/([^/\.]+)/?$ index.php?action=$1 
RewriteRule ^m/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&page=$2 [L]
#ErrorDocument 404 /not_found.php

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*)$ https://example.com/$1 [R=301,L]

Thanks in advance for any help or pointers given

Upvotes: 1

Views: 717

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

The first thing is that you need all of your redirects to happen before your routing rules. Then, you can simply add a condition to the rule that redirects to HTTPS:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^(.*/)?email/image/.+\.png$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*)$ https://example.com/$1 [R=301,L]

# Dont send these requests to index.php:
RewriteRule ^(ajax|test)($|/) - [L]

RewriteRule ^m/([^/\.]+)/?$ index.php?module=$1 
RewriteRule ^a/([^/\.]+)/?$ index.php?action=$1 
RewriteRule ^m/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&page=$2 [L]
#ErrorDocument 404 /not_found.php

Note that a rewrite condition only applies to the immediately following rule. So you have to specifically match conditions to a specific rule by putting them right before the rule.

Also, it looks like your email image regex might be wonky. It only matches stuff like this: /email/image/.png (e.g. no filename).

Upvotes: 2

Related Questions