xain
xain

Reputation: 13839

Redirect not working in apache

I configured the following redirection:

<VirtualHost ...>
  <Directory ...>

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(myapp/login) https://%{HTTP_HOST}:443/$1 [NC,R=301,L]

But it's not redirecting. How can I troubleshoot it ?

Upvotes: 1

Views: 4502

Answers (3)

Wrikken
Wrikken

Reputation: 70490

General debugging: if you have access to the apache configuration itself, you can enable a RewriteLog which, depending on which level you set, can give you a step-by-step breakdown of matches, current values, and alterations.

What is going on here is that there's a difference in the url matching in en entire host context (only available in apache's configration files), or per-directory of .htaccess context:

What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).

The difference between the two being that in a VirtualHost context you have a leading /, in .htcaccess context (wich most examples on the net assume because it's more readily available) you don't have that leading /.

In short, here this works, but wouldn't work in .htaccess:

^/(myapp/login)

And if you want to make it match in both contexts, make the leading / optional:

^/?(myapp/login)

Upvotes: 2

Aaron Saray
Aaron Saray

Reputation: 1177

For me, the following code worked (I think this has to do with a combination of the != (as @jason said) and the port.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(myapp/login) https://%{HTTP_HOST}/$1 [R=301,L,NC]

Upvotes: 1

Jason Blevins
Jason Blevins

Reputation: 688

I think there may be some problem in negating the lexicographic equality operator. I wasn't able to find anything prohibiting this in the mod_rewrite documentation or elsewhere, but I also did not see examples using !=. In fact, the documentation indicates that it should work, but you might try just matching "off" instead of "not on", like this:

RewriteCond %{HTTPS} off

If this is the case, it would be very difficult to troubleshoot using the usual methods (e.g., using RewriteLog).

Upvotes: 0

Related Questions