Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] Using mod_rewrite in https, not mod_rewrite to https

Started by Mathachew Jul 16, 2008 at 2:01 PM 12 replies 3.8k views
Original Post
Mathachew
Mathachew
I've got a domain that I'm forcing www and https on. When accessing domain.com, everything is rewriting perfectly. However, the issue I'm having is to force https://domain.com To rewrite to https://www.domain.com The settings currently in place:

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]
I added RewriteLog and set the level to 9 so that everything was logged for the port 443 entry, but the log is not updated. To reiterate, the problem is forcing https://domain.com to https://www.domain.com. I can't force www and then http to https when https is being initially connected to.
Oluseyi
Oluseyi
This should do the trick:
Redirect / https://www.domain.com/
Mathachew
Mathachew
That bit causes a redirect loop in my SSL config settings. Exactly where do you suggest I put this?
Oluseyi
Oluseyi
Put it inside the non-SSL virtual host declaration. Putting it at the global level will cause the infinite redirections, like you're experiencing.
Mathachew
Mathachew
Either I'm not making my problem clear or people are glancing over what I'm describing as the problem since this is the third time a solution for port 80 was suggested when the problem is located on port 443. While the redirect can help me reduce the rewrite rules I have to force www and https (I intend on using it, thanks), I need the site to go to https://www.domain.com when https://domain.com is accessed.
Sander
Sander
Are you sure that the logfile path you set with RewriteLog is writable by Apache? Is it an absolute path? You could also touch the logfile yourself and chown it to the proper user.
Mathachew
Mathachew
The path to the log file is correct because I could see it populating (tail -f) as I tried accessing it on port 80 and 443. I tried throwing :443 on the end of the condition, which would trigger a redirect loop because www.domain.com != www.domain.com:443.
Sander
Sander
Quote:
The path to the log file is correct because I could see it populating (tail -f) as I tried accessing it on port 80 and 443.


Ah. Your original post said that the logfile stayed empty. Could you post part of the rewrite log then please?
Mathachew
Mathachew
Well there's nothing to post when using the rewrite I have in place. Want me to add :443 and show you want it does? The current condition is not met so I have to force it to not match in order for the rewrite to apply.
Sander
Sander
Quote:
Well there's nothing to post when using the rewrite I have in place.


Eh? That can't be. With RewriteLogLevel set to 9 it should take Apache well over 100 lines per request to not match a simple rule. When the Apapche guys warn you about RewriteLogLevel 3 and up being verbose enough to kill your server in a live environment, they are not kidding.

Are you sure you are looking at the file set by RewriteLog, and not the standard Apache access log or error log?
Mathachew
Mathachew
Yep, I'm positive the file is correct.

The following SSL config does not update the rewrite log:
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]


The following does:
RewriteCond %{HTTP_HOST} !^www.domain.com:443$ [NC]RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]


The only difference is the :443 on the end. Realistically, you can be anything on the end and it would yield the same results (a friend suggested trying it with 443 since the problem lies in the SSL config, not the port 80 config). Here's another baffling event of logging, or lack thereof. My original configuration would not cause the log to update (even with the level at 9). My port 80 configuration, which did not have the log enabled, was updating the rewrite log. The following may be off in a spot or two because I'm going by memory since this configuration is no longer in place (Port 80 connections automatically redirect to 443 instead of using mod_rewrite). This is the gist of what I had:

<VirtualHost *:80>    ServerAdmin webmaster@domain.com    DocumentRoot /path/to/domain.com    ServerName domain.com    ServerAlias *.domain.com    RewriteEngine on    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]    RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]    RewriteCond %{HTTPS} off    RewriteRule ^(.*) https://%{HTTP_HOST} [R,L]</VirtualHost><VirtualHost 67.192.103.223:443 192.168.100.223:443>    ServerAdmin webmaster@domain.com    DocumentRoot /path/to/domain.com    ServerName domain.com    ServerAlias *.domain.com    LogLevel warn    RewriteEngine on    RewriteLog /var/log/httpd/domain.com_rewrite_log    RewriteLogLevel 9    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]    RewriteRule ^(.*) https://www.domain.com$1 [R,L]</VirtualHost>



My assumption would be that the rewrite log would only update when rewriting occurred on port 443 since that is the only configuration that says to use it, but hey, I've been wrong before!
Sander
Sander
Quote:
Realistically, you can be anything on the end and it would yield the same results


Interesting. Perhaps modify your condition to:

RewriteCond %{HTTP_HOST} !^www.domain.com [NC]

The difference is that I dropped the $ at the end. $ matches the end of a string, so you end up matching the whole HTTP_HOST. With this modified condition you only match the start.

Mathachew
Mathachew
It was worth a shot, but still no dice. Log also does not update when trying it without $ on the end. The log does update when accessing the proper URL. Here's what was listed in case it reveals anything:

[18/Jul/2008:14:58:45 --0500] [www.domain.com/sid#85bc170][rid#8d36ee8/initial] (2) init rewrite engine with requested uri /index.php[18/Jul/2008:14:58:45 --0500] [www.domain.com/sid#85bc170][rid#8d36ee8/initial] (3) applying pattern '^(.*)' to uri '/index.php'[18/Jul/2008:14:58:45 --0500] [www.domain.com/sid#85bc170][rid#8d36ee8/initial] (4) RewriteCond: input='www.domain.com' pattern='!^www.domain.com' => not-matched[18/Jul/2008:14:58:45 --0500] [www.domain.com/sid#85bc170][rid#8d36ee8/initial] (1) pass through /index.php
Sander
Sander
Three things you can try:

1) Enable rewrite logging on port 80 as well and log to a different logfile. See if your https request is accidentally picked up by the other host (unlikely, but worth a shot).

2) modify your condition rule to:

RewriteCond %{HTTP_HOST} ^.*www [NC]

Perhaps there is something in front of the www when on https. Again, unlikely but worth a shot.

3) Apache does not support name-based virtual hosting on https (there is a way around that). So, all your requests on port 443 always end up in the same virtual host config. Make sure that your host is the *only* host on port 443 for these IP addresses. Make sure that there is no "default" host in your configuration anywhere in the standard Apache configuration.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.