Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

(OT) mod_rewrite anyone?

by mrc (Sexton)
on Sep 26, 2009 at 10:57 UTC ( #797673=perlquestion: print w/ replies, xml ) Need Help??
mrc has asked for the wisdom of the Perl Monks concerning the following question:

I'm using mod_rewrite to virtually map to a local path.

path:
/home/user/subdomain.domain.com/

Rules:
RewriteEngine On RewriteCond %{HTTP_HOST} \.([^.]+\.[^.]+\.[^.]+)$ RewriteRule ^(.*) http://%1$1 [R,L] VirtualDocumentRoot /home/user/%0
RewriteCond %{HTTP_HOST} \.([^.]+\.[^.]+\.[^.]+)$ will redirect http://www.subdomain.domain.com to http://subdomain.domain.com, but I want the reverse. http://subdomain.domain.com redirected to http://www.subdomain.domain.com and 'www' url to remain unchanged.
Changing the rule to RewriteRule ^(.*) http://www\.%1$1 [R,L] will broke entire configuration. I have also tried to modify RewriteCond %{HTTP_HOST} \.([^.]+\.[^.]+\.[^.]+)$ in many ways, but I don't know how to solve it.
Any help would be greatly appreciated. Thank you!!

Comment on (OT) mod_rewrite anyone?
Select or Download Code
Re: (OT) mod_rewrite anyone?
by moritz (Cardinal) on Sep 26, 2009 at 11:02 UTC
    Maybe
    RewriteCond %{HTTP_HOST} !^www RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R,L]

    (Don't know if you can interpolate these variables in URLs)

    Perl 6 - links to (nearly) everything that is Perl 6.
      Not working, sadly.
        It would help if you were more specific.
        Perl 6 - links to (nearly) everything that is Perl 6.
Re: (OT) mod_rewrite anyone?
by clinton (Priest) on Sep 26, 2009 at 11:02 UTC

    Firstly, this has nothing to do with Perl.

    Secondly, if you look at the docs for apache, you will find a link to the URL rewriting guide, and the second entry Canonical Hostnames explains how you would achieve this.

    Clint
      Firstly, this has nothing to do with Perl.
      Hmm :( Regexes are very similar to perl.
        Nice try ;)
      ...and the second entry Canonical Hostnames explains how you would achieve this.
      Yes but I need a wildcard. I tried something like
      RewriteCond %{HTTP_HOST} !^www\.%1 [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/?(.*) http://www.%1/$1 [L,R,NE]
      but not working. These rules are so hard :(
Re: (OT) mod_rewrite anyone?
by merlyn (Sage) on Sep 26, 2009 at 16:02 UTC
    Why are you still using mod_rewrite? I gave that up the moment I discovered I could use mod_perl to do it far easier and better, and with a real language, instead of a language involving yet another arcane syntax.

    Just set up a PerlTransHandler that modifies the right strings, and you're done.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      Thanks for suggestion.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://797673]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-09-16 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls