You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Octavian Hornoiu <oc...@hacknslash.org> on 2005/04/04 23:35:08 UTC

[users@httpd] using mod_rewrite for subdirectories

Hi, I am new to using mod_rewrite but I have used apache extensively in
the past.  I am interested in redirecting sub-directories to a host name
in the same domain.  Here is an example:

http://my.domain.com/externaldomain/  >  http://externaldomain.domain.com/

Here is another example:

http://www.test.com/firefox/ > http://firefox.test.com/


How would i go about writing the rule to match this and redirect this in
the virtual host container for www.test.com?  I have tried several
rulesets with no success so I'm clearly not understanding something
correctly.  I would also like to match against the trailing slashes in
case the users requested a file by mistake instead of the directory.


Also, how do you place multiple rulesets and redirects in the same
virtual host container?  If i want to redirect 20-30 of these
sub-directories how do I list them in the ruleset?

Thanks for your help!

Octavian Hornoiu

Re: [users@httpd] using mod_rewrite for subdirectories

Posted by Kevin O'Neil <ke...@verizon.net>.
----- Original Message ----- 
From: "Octavian Hornoiu" <oc...@hacknslash.org>
To: <us...@httpd.apache.org>
Sent: Monday, April 04, 2005 5:35 PM
Subject: [users@httpd] using mod_rewrite for subdirectories


> Hi, I am new to using mod_rewrite but I have used apache extensively in
> the past.  I am interested in redirecting sub-directories to a host name
> in the same domain.  Here is an example:
>
> http://my.domain.com/externaldomain/  >  http://externaldomain.domain.com/
>
> Here is another example:
>
> http://www.test.com/firefox/ > http://firefox.test.com/
>
>
> How would i go about writing the rule to match this and redirect this in
> the virtual host container for www.test.com?  I have tried several
> rulesets with no success so I'm clearly not understanding something
> correctly.  I would also like to match against the trailing slashes in
> case the users requested a file by mistake instead of the directory.
>
>
> Also, how do you place multiple rulesets and redirects in the same
> virtual host container?  If i want to redirect 20-30 of these
> sub-directories how do I list them in the ruleset?
>
> Thanks for your help!
>
> Octavian Hornoiu
>







I found this to work and use it on my server:

RewriteEngine On

Options +FollowSymlinks

RewriteBase /

RewriteCond %{HTTP_HOST} subdomain.yoursite.com

RewriteCond %{REQUEST_URI} !foldername/

RewriteRule ^(.*)$ foldername/$1 [L]

Foldername should be the same as subdomain.

If this is incorrect, please let me know.

Thanks,
Kevin







--------------------------------------------------------------------------------


> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org 



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] using mod_rewrite for subdirectories

Posted by Noah <si...@onastick.net>.
On Mon, Apr 04, 2005 at 02:35:08PM -0700, Octavian Hornoiu wrote:
> Hi, I am new to using mod_rewrite but I have used apache extensively in
> the past.  I am interested in redirecting sub-directories to a host name
> in the same domain.  Here is an example:
> 
> http://my.domain.com/externaldomain/  >  http://externaldomain.domain.com/
> 
> Here is another example:
> 
> http://www.test.com/firefox/ > http://firefox.test.com/

Assuming I understand what you're going for here...

RewriteEngine on
RewriteRule ^/([^/]+) http://$1.test.com/ [L,R]

This will capture the first part of the URL path and use it as the
hostname in your domain. It will NOT, in this form, add any other
pathing info; you *could* have something like this:

RewriteEngine on
RewriteRule ^/([^/]+)(/.*)? http://$1.test.com$2 [L,R]

...which would append any of the URI path data *after* the initial part
to the the target. This may or may not be in keeping with what you have
in mind, but I figured I'd toss it out there.

> Also, how do you place multiple rulesets and redirects in the same
> virtual host container?  If i want to redirect 20-30 of these
> sub-directories how do I list them in the ruleset?

Well, if it's always going to be the case that www.test.com/PATH maps to
http://PATH.test.com/, the above rule will handle all the cases. If
there are other cases that need handling, you can add rules for them;
note that order *may* matter, depending on the nature of the rules.

--n

-- 
"Any system shall not harm your content or, through 
inaction, allow your content to come to harm."
                            - Jeff Raskin

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org