jeudi 10 décembre 2015

Apache 2.4 redirect all to https except specific url

I would like to redirect all pages for a domain from http to https except for a specific path containing 'tsystem' in the url. The url has also a %{QUERY_STRING} part. The url is like http://ift.tt/1Z0ERmY and it should be the always in http. So in the virtualhost I put the lines:

<VirtualHost *:80>
    ...
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/tsystem [NC]
    RewriteRule ^(.*)$ https://myhost.test$1 [QSA,NC,R=301,L]
    ...
</VirtualHost>

From the logs I saw that the RewriteCond is right matched but the RewriteRule, even if there is ^(.*)$ as patthern, remove the %{REQUEST_URI}'s url part and concatenates only the %{QUERY_STRING}'s url part. So I obtain http://ift.tt/1HW89Pg

From the docs apache mod_rewrite I read:

In mod_rewrite, the NOT character ('!') is also available as a possible pattern prefix. This enables you to negate a pattern; to say, for instance: ``if the current URL does NOT match this pattern''. This can be used for exceptional cases, where it is easier to match the negative pattern, or as a last default rule.

Note When using the NOT character to negate a pattern, you cannot include grouped wildcard parts in that pattern. This is because, when the pattern does NOT match (ie, the negation matches), there are no contents for the groups. Thus, if negated patterns are used, you cannot use $N in the substitution string!

but I use the negation match in the RewriteCond not in the RewriteRule. So where am I wrong?

I also tryed

<VirtualHost *:80>
    ...
    <If "! %{REQUEST_URI} =~ /tsystem/">
        RewriteRule ^(.*)$ https://myhost.test$1 [QSA,NC,R=301,L]
    </If>
    ...
</VirtualHost>

I thought that in this manner I only match the url with no "/tsystem/" inside, and the url with "/tsystem/" inside could be processed in the virtualhost *:80. For url with no "/tsystem/" inside the rule is correct but with "/tsystem/" inside I obtain the url http://ift.tt/1HW88ep

So is there a way to bypass an https redirect for a specific url with a %{QUERY_STRING} part attached? And that url can be maintained the same?

Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire