Koozali.org: home of the SME Server

ModRewrite [solved]

Offline groovicus

  • *
  • 12
  • +0/-0
ModRewrite [solved]
« on: April 03, 2007, 01:17:02 AM »
I can never find the answer until 30 seconds after I ask, so here goes. Is there a built in means of changing the Document Root for Virtual Hosts? I am supposing that I will have to use a template modification, but if that is the case, can one use an 'unless' directive to make sure only the Host I want is directed to the new DocumentRoot like one would do for a rewrite rule?

Or would the better option be to add the rewrite rule? If I change the Document Root, I can configure Tomcat to serve the correct .jsp page. If I use the rewrite rule, I can just load the correct page. Seems six of one, half a dozen of the other to me.

EDIT: I went the path of a rewriting the rule. Something is not quite right with it. Can anybody see the issue?
Code: [Select]

RewriteCond %{HTTP_HOST} (.*)http://www.mydomain.com [NC]
RewriteRule ^(.*)  http://www.mydomain.com/myservlet/

Offline groovicus

  • *
  • 12
  • +0/-0
Re: Set documentroot for virtual domains
« Reply #1 on: April 03, 2007, 04:12:19 AM »
accidental dupe

Offline groovicus

  • *
  • 12
  • +0/-0
ModRewrite [solved]
« Reply #2 on: April 07, 2007, 05:27:19 PM »
After 5 days of reading the Mod_Rewrite documents, and a little bit of time refreshing myself on how Perl handles Regex, I finally came up with my rewrite rule that works. What I was trying to do was create a rewrite rule that rewrote www.example.com to www/example.com/my_servlet/. The part that made it sort of hard was that I have multiple domains, so I had to make sure to exclude those and not break anything else.

So step by step:
Create a new directory:
mkdir /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/VirtualHosts/

The second thing was to copy /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/VirtualHosts/26RewriteTraceAndTrack to my newly created directory.

I opened 26RewriteTraceAndTrack, and made it look like this:
Code: [Select]

{
    $OUT =<<'HERE';


    RewriteEngine on
    RewriteLog /etc/httpd/logs/rewrite.log
    RewriteLogLevel 1
   
    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{HTTP_HOST} !^(.*)www\.example2.com(.*)
    RewriteCond %{REQUEST_URI} !^(.*)webmail(.*)
    RewriteRule ^(.*)$ http://www.example1.com/my_servlet/ [L]
 
   
HERE
}

The first three lines are pretty self explanatory; turn on the rewrite engine, tell where the rewrite.log is located, and set logging level (9 is the highest).

The first RewriteCond is to make sure that there is nothing after the domain name. I had a hard time with this part because I didn't realize that even if the REQUEST_URI was blank, it still held a back-slash.

The second rewrite condition was to make sure that I excluded all other domains except the one that I want. I only show one example here, but the rule could be repeated for multiple exclusions.

The third rewrite rule is now redundant. I added it in there to make sure I excluded any domains that were in the format of www.example.com/webmail.I didn't want to accidentally break access to my webmail. In reality, this condition could be removed.

The rewrite rule says, in a nutshell, no matter what I have, replace it with
http://www.example1.com/my_servlet/. Because of the previous conditions that excluded all other possibilities, I know what I have left is what I want to replace. The [L] at the end signals mod_rewrite to not proess any more rules.

Then I did:
expand-template /etc/httpd/conf/httpd.conf
/etc/rc.d/init.d/httpd-admin restart
/etc/rc.d/init.d/httpd-e-smith restart


I'm not sure if those last two lines are the proper way to restart apache, but it doesn't seem to have broken anything to this point.

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
ModRewrite [solved]
« Reply #3 on: April 08, 2007, 10:15:00 AM »
Quote from: "groovicus"
Then I did:
expand-template /etc/httpd/conf/httpd.conf
/etc/rc.d/init.d/httpd-admin restart
/etc/rc.d/init.d/httpd-e-smith restart


I'm not sure if those last two lines are the proper way to restart apache, but it doesn't seem to have broken anything to this point.
I believe it is the proper way.
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)