Why won't my mod_rewrite work for HTTPS like HTTP
I'm sorry for yet another of these posts, but from what I can tell, my question is different than the prevailing http -> https redirects out there.
I want to
redirect all http://www.mydomain.com traffic to https://www.mydomain.com/wiki
AND
redirect https://www.mydomain.com to https://www.mydomain.com/wiki
Notice the https in my first redirect goal.
For the first redirect, I am able to accomplish this by putting:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In my httpd.conf file and restarting.
I thought this would also work for my https:// attempts. Notice, I am not including
RewriteCond %{HTTPS} !=on
or anything like that. Yet, still https://www.mydomain.com sends me to my index.html file in my server root.
If I try to put the above Rewrite directives in my httpd-ssl.conf file and restart the server then I get infinite redirects.
What am I doing wrong?
NOTE: For what its worth, /wiki is an alias to /home/Users/myusername/www/wiki (the absolute path to wiki)
UPDATE
Rehash as to what I've tried so far:
Attempt 1:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
Nothing Rewrite related
Result 1:
Redirects all http traffic to https://www.mydomain.com/wiki Does 开发者_开发问答nothing for https://www.mydomain.com
Attempt 2:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
Result 2:
Infinite redirects.
Attempt 3:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule !^wiki https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule !^wiki https://%{SERVER_NAME}/wiki [R,L]
Result 3:
Infinite redirects.
Your rule will match every request (even /wiki
). Try to exclude that:
RewriteRule !^wiki https://%{HTTP_HOST}/wiki [R,L]
The infinite redirects are coming from the fact that the rule matches both the first client request (eg. http://...) and from any subsequent request.
You would do better to use a rewrite rule which excludes the wiki path from the match e.g.
RewriteRule !^wiki https://%{HTTP_HOST}/wiki [R,L]
or you might get better performance from a RewriteCond
RewriteCond %{REQUEST_URI} !^wiki
before your existing rule. This will make it easier to add more complex patterns that you currently have in your example (should you need to later in your project).
I found a solution that might not be the most robust but it works for me.
In my httpd-ssl.conf file, under the VirtualHost container, I set the DocumentRoot to point to the directory that I've been trying to redirect to.
<VirtualHost _default_:443>
DocumentRoot "path on my HD where I wanted to redirect all along"
ServerName All the usual stuff...
...
This takes care of "redirects" (not really redirects anymore) for https transactions
For http (non-ssl) I placed in httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
That seemed to do it.
精彩评论