Forum Moderators: phranque

Message Too Old, No Replies

how to mod_rewrite http to https?

         

gary_new

10:24 am on Feb 3, 2004 (gmt 0)

10+ Year Member



I am trying to write a general set of rewrite rules that will rewrite a simple virtual host to a secured URI.

Example:

[domain.tld...] = /home/domain.tld/htdocs

Rewrite:

[secure.provider.tld...] = /home/domain.tld/htdocs

I've developed a simple set of rules that will display the index page under the secured URI. The problem is while pre-processed includes (i.e., php includes) are rewritten correctly, I am having difficulties with the post-processed xhtml objects (i.e., images and css) not being rewritten correctly, getting 404 on the images and css directories.

Apache Log:

"GET /secure/domain.tld/ HTTP/1.1" 200 26209
"GET /css/domain.css HTTP/1.1" 404 297
"GET /images/domain.png HTTP/1.1" 404 300

My image and css references are relative links and all references are under a single root directory.

Example:

<img src="/images/domain.png" />

My goal is to put together a rewrite rule that will rewrite the entire root directory to include subdirectories. I would assume something like this would be quite basic. I would appreciate your suggestions and any examples would be great, too.

Thank you for your assistance.

Respectfully,

Gary

jdMorgan

2:58 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gary,

Welcome to WebmasterWorld [webmasterworld.com]!

> I would assume something like this would be quite basic.

Because of the fact that servers can be configured in endlessly-varying ways, that assumption is usually not correct.

You may post your code - with any domain-specific information replaced by "example.com" - if you're asking for help with it. However, in accordance with our charter [webmasterworld.com], we prefer not to write your code for you, but rather to help you write it.

Jim

gary_new

7:45 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



Here is the basics of what I have so far:

Original URI = [domain.tld...]

Rewritten URI = [secure.provider.tld...]

RewriteEngine On
RewriteRule ^/secure/(.*\.+.*)/(.*) /home/$1/htdocs/$2

As I stated before, all pre-processed objects (i.e., php includes) are executed fine within the index file. It is the post-processed objects (i.e., images and css) that are getting the 404.

Any help would be greatly appreciated.

Respectfully,

Gary

jdMorgan

8:42 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gary,

In order to get to your secure server, you're going to need to do an external redirect:


RewriteRule ^/secure/([^.]*\.[^/]*)/(.*)$ https://www.secureprovider.tld/home/$1/htdocs/$2 [R=302,L]

Your regular-expressions syntax for the $1 back-reference was questionable, and I have taken a best-guess at what was intended. Try to avoid ".*" in the middle of patterns unless absolutely necessary or unambiguous - It is inefficient. The pattern shown says "'/secure/' followed by any number of characters except for '.', followed by '.', followed by any number of any characters except a slash, followed by a slash, followed by any number of any characters." That may or may not be what you intended; If not, maybe it can serve as an example of how to avoid '.*' sequences in the middle of patterns.

I have shown a 302-Moved Temporarily redirect here, assuming that you don't want search engines to index the secure-server page, but rather, the page on your regular server. If you do want the secure page(s) indexed, use a 301.

Jim

gary_new

12:24 am on Feb 4, 2004 (gmt 0)

10+ Year Member



Jim,

My rule attempts to match the /secure/ subdirectory and then match any following string as long as it has at least 1 dot somewhere in it (i.e., a domain.tld).

Doing an external redirect is not my intention. I want to be able to securely or unsecurely access the same vhost's documentroot with a secure and unsecure URL.

I believe I discovered the reason for the post-processed objects (i.e., images and css) returning 404. I modified the src of each object to not include an initial slash.

Example:

<img src"images/domain.png />

This corrected the problem for the initial index page, but then opened a new can of worms for pages in deeper directories. My pages are dynamically generated with includes and uses the same src reference no matter how deep it goes into the site.

I believe what must be done is either find a way to dynamically change the virtualdocumentroot or use a include file before each of my post-processed objects to determine whether it is a http or https connection and for the URI accordingly. I would prefer the former.

I will begin a new thread which attempts to address it.

Respectfully,

Gary