Forum Moderators: phranque

Message Too Old, No Replies

Switching from <VirtualHost> configuration to mod rewrite

         

hoggwild

7:04 pm on Mar 30, 2008 (gmt 0)

10+ Year Member



Hi,

I'm new to the forums, and a novice at Apache. I have a WAMP server for my personal use, plus running a couple of websites of my own, set up at home using Windowx XP Pro SP2 and Apache 2.2.6. I currently have several VirtualHost directives in my http.conf file; however, I'd like to switch this to a dynamic hosting situation where I can make changes to add domains and/or subdomains without having to change the configuration file and restart Apache every time (I travel quite a bit and would like to do this from the road without having to restart the server).

In addition, I have a couple of sites where I'd like to switch from dynamic to static urls using mod-rewrite directives in .htaccess files (these sites run MediaWiki and phpBB).

I've read quite a bit of info on this and I'm thoroughly confused, and all attempts to make this work have been futile b/c I'm sure I'm doing it wrong. I've managed to get the dynamic sites piece working using mod_vhost_alias and VirtualDocumentRoot but any attempts to use .htaccess to rewrite dynamic to static urls resulted in a recursive loop.

I have a common directory schema where each domain (www.mysite.com) maps to /home/mysite/www/, and there is an existing <VirtualHost> directive in my .conf file for each site, so I think that step is in place.

How do I go about switching these sites over to a dynamic basis? I'm not concerned about separate log files b/c they're all my sites and I don't need that. I'd just like to add sites and configure static urls using .htaccess without having to restart Apache.

Any assistance would be greatly appreciated. If there is a post on this I apologize -- please feel free to point me to it; I searched and read several but did not find an answer.

Thanks again!

g1smd

7:43 pm on Mar 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code have you got for your rewrite?

By the way, your description is back to front.

The rewrite connects a static-looking URL, to a dynamic internal filepath.

In that case, the URLs in the links on your site need to point to the static-looking format.

You'll also need a 301 redirect from the dynamic-looking URLs to the static-looking URLs in order to avoid Duplicate Content issues.

There are many examples of such code to use as a basis to get started.

hoggwild

11:32 am on Mar 31, 2008 (gmt 0)

10+ Year Member



g1smd -- thanks for your response, and correcting my description. :)

Background: I'm using "G:/home" as the DocumentRoot for this installation.

I finally got the rewrite for the dynamic virtual hosts to work using the following in my httpd.conf file:

UseCanonicalName Off

#Modifies log file to support virtual host splitting
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog "G:\Logs\access_log" vcommon

RewriteEngine On
RewriteMap LOWERCASE int:tolower

#Translate hostname to site name
RewriteCond ${LOWERCASE:%{HTTP_HOST}} ^(www\.)?(.*)$
RewriteRule ^/(.*)$ /%2/www/$1 [L]

I had to change my directory schema from /home/mysite1/www to /home/mysite1.com/www, which is fine (I just could not get it to work the other way). This all maps great. For each site, I get the public web directory /www.

Now, I'm trying to get the following to happen for one of the sites by configuring .htaccess with additional rewrites (these would be site specific, and may differ from site to site):

1. When a user request URL "http://www.mysite.com", they get redirected to "http://www.mysite.com/wiki", which will pull up "http://www.mysite.com/wiki/Main_Page".
2. "http://www.mysite.com/wiki/Main_Page" is actually a static-looking url that maps to "http://www.mysite.com/w/index.php?title=Main_Page". Other pages in the wiki will need to map in a similar fashion using a rewrite of "w/index.php?title=$1" to a static-looking URL.

I've tried the following in the .htaccess file for mysite.com (placed in the /www directory), but get error messages that I can't figure out how to fix:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki/*$ wiki/ [L,QSA]
RewriteRule ^/*$ wiki/ [L,QSA]

My Apache error log contains the following:

....File does not exist: G:/home/mysite.com/www/mysite.com
....File does not exist: G:/home/mysite.com/www/error

I'm stabbing in the dark here, not sure how to go about changing the rewrites in the .htaccess file to make this work.

Any assistance would be greatly appreciated.

jdMorgan

2:43 pm on Mar 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can end up with a port number in the path with that hostname pattern, and the rule will recurse on redirection. You might want to try:

# Translate hostname to site name
RewriteCond ${LOWERCASE:%{HTTP_HOST}} ^(www\.)?([^.]+\.[^:]+)(:[0-9]+)?$
RewriteCond $1 !^([^/]+)/www/
RewriteRule ^/(.*)$ /%2/www/$1 [L]

Comments:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki/*$ /wiki/ [L,QSA]
RewriteRule ^/*$ /wiki/ [L,QSA]

RewriteConds apply only to the single RewriteRule which follows them. Is that what you want here?
If the intent of the last two rules is to "fix-up" trailing slash problems, then these two rules should invoke an external redirect to inform the client (you can construct the correct hostname for the redirection URL by using the %{HTTP_HOST} variable).
The second rule will loop on itself, because "/wiki/" will match the pattern. You'll need to change that pattern to "^wiki(//+)?$" to fix a missing trailing slash or more than one slash, but not affect URL-paths with a (correct) single slash.

Note the leading slashes prepended to the substitution paths, as shown in the Apache URL Rewriting Guide.

[QSA] is only required if you wish to *add* query data to the URL-path in the rule itself. It is not needed if you wish to replace the existing query data, or if you wish to pass it through the rule unchanged.

You're involved with a fairly complex project here, so go slow and do a lot of research and study before committing to any one approach -- and especially before adding a ton of new domains!

Jim

hoggwild

6:09 pm on Mar 31, 2008 (gmt 0)

10+ Year Member



jdMorgan -- thank you so much for your commentary and suggestions. I finally got the results I was looking for. I also made the changes you suggested to the httpd.conf rewrite instructions.

I put the following in the .htaccess file located in the root web directory of [mysite.com,...] which is g:\home\mysite.com\www\


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki(//+)?$ /wiki/ [L,QSA]
RewriteRule ^/*$ /wiki/ [L]

Now, I'm going through the book I bought on mod_rewrite to see if I understand what's been done.

Thanks so much for your help with this. I really appreciate it!

jdMorgan

2:05 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Now, I'm going through the book I bought on mod_rewrite to see if I understand what's been done.

LOL! Well, it's usually done in the reverse order, but good on ya!

Jim