Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite subdomain with subdir

mod_rewrite subdomain with subdir

         

pooyara

9:40 am on Sep 27, 2007 (gmt 0)

10+ Year Member



I am trying to achieve this
http://example.com/?action=xx2&user=xx1 to
http://xx1.example.com/xx2

i can
http://example.com/?user=xx1 to
http://xx1.example.com

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING}!user=[^&]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*) /$1?user=%2 [L]

[edited by: jdMorgan at 12:13 pm (utc) on Sep. 27, 2007]
[edit reason] example.com [/edit]

jdMorgan

12:05 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is tough because the back-references to RewriteConds are only available for the last-matched RewriteCond. Therefore, it is necessary to create all the back-references in one RewriteCond, or to use multiple rules.

One way to do it using a single rule:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} !&?user=[^&]+
RewriteCond %{HTTP_HOST}<->%{QUERY_STRING} ^(www\.)?([^.]+)\.example\.com(:[0-9]{1,5})?<->([^&]+&)*action=([^&]+)
RewriteRule (.*) /%5/$1?user=%2 [L]

This rewrites <user>.example.com/<resource>?user=bob&action=go to /go/<resource>?user=bob

Notes:

The subpattern following the HTTP_HOST subpattern is intended to recognize an optional (and valid) port number which may be appended to the hostname.

The <-> character sequence is arbitrary; While I use it to imply concatenation, it really only serves as a 'boundary marker' between the two combined values that we are trying to match in the RewriteCond. You could replace this with any character or character string as long as it is unique and cannot appear in a valid hostname.

Jim