Forum Moderators: phranque

Message Too Old, No Replies

Grabbing more than I want

mod rewriting subdomain and directories

         

JeNeSaisLaw

9:17 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



I have the following code and it is not working (more on what I mean to follow):

### Reserved Subdomains ###
RewriteCond %{HTTP_HOST} RewriteCond %{HTTP_HOST} ^(groups如ortal在oards妃y夙uide好ews)\.example\.com(/)?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^(.*) index.php?l=%1&s=$1&sub=$2 [L,QSA]

With this code, the following URLs work:
#1 [portal.example.com...]
#2 [portal.example.com...]

#1 rewrites to index.php?l=portal&s=&sub=
#2 rewrites to index.php?l=portal&s=users&sub=

However, the following URLs do not work:
#3 [portal.example.com...]
#4 [portal.example.com...]

#3 rewrites to index.php?l=portal&s=users/&sub=
#4 rewrites to index.php?l=portal&s=users/0607&sub=

#3 should rewrite to index.php?l=portal&s=users&sub=
#4 should rewrite to index.php?l=portal&s=users&sub=0607

It seems to me that the first "([A-Za-z0-9-]+)?/" is grabbing too much for $1 ($1 = "users/" and "users/0607" instead of just "users"), despite only allowing A-Za-Z0-9-.

However, I have no idea why this ($1 representing everything after the trailing / after example.com) is happening. Any ideas?

[edited by: JeNeSaisLaw at 9:26 pm (utc) on June 5, 2008]

[edited by: jdMorgan at 12:30 am (utc) on June 6, 2008]
[edit reason] One more example.com [/edit]

jdMorgan

12:35 am on Jun 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first line/lines of code seem to be corrupted or mis-copied and appears to be mis-coded. Can you clarify the specific intent of these lines:

RewriteCond %{HTTP_HOST} RewriteCond %{HTTP_HOST} ^(groups如ortal在oards妃y夙uide好ews)\.example\.com[i](/)?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?[/i]

I see two RewriteConds on one line -- the first one with a blank pattern, and an apparent attempt to match part of a URL-path against the hostname (italicized characters), which won't work.

Jim

jdMorgan

12:42 am on Jun 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another point of apparent confusion: $1 refers to the string matching the first parenthesized sub-pattern in the RewriteRule, while $2 refers to the string matching the second parenthesized sub-pattern in the RewriteRule.

%1 refers to the string matching the first parenthesized sub-pattern in the last-matched RewriteCond, while $2 refers to the string matching thesecond parenthesized sub-pattern in the last-matched RewriteCond.

Therefore, your $1 is working as it should, while $2 is blank, and %1 and %2 will both be empty, because the URL-path is never present in the HTTP_HOST variable.

Jim

JeNeSaisLaw

4:27 am on Jun 6, 2008 (gmt 0)

10+ Year Member



Oops, not sure how I messed that copying up.

Here is the correct, current code:

### Reserved Subdomains ###
### Line one is supposed to grab the subdomain only if it matches one of groups, portal, boards, my, guide, or news. It is also supposed to grab the first and second set of alphanumeric characters (and hyphens) that trail example.com
RewriteCond %{HTTP_HOST} ^(groups如ortal在oards妃y夙uide好ews)\.example\.com(/)?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?
### Line two is supposed to prevent a loop
RewriteCond %{REQUEST_URI} !^/index.php.*
### Line three is supposed to take the content of the subdomain, set it to "l" (with %1), and then set the first and second set to "s" and "sub", respectively.
RewriteRule ^(.*) index.php?l=%1&s=$1&sub=$2 [L,QSA]

Currently, this works for both "l" and "s", but not "sub". $2 is blank.

[edited by: JeNeSaisLaw at 5:11 am (utc) on June 6, 2008]

JeNeSaisLaw

4:26 pm on Jun 6, 2008 (gmt 0)

10+ Year Member



Ok, it took a night to sleep on it, but I understand why you made the last post now. I have it working, thank you.

For future reference for people:

RewriteCond %{HTTP_HOST} ^(groups¦portal¦boards¦my¦guide¦news)\.example\.com
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?/? index.php?l=%1&s=$1&sub=$2 [L,QSA]

jdMorgan

3:20 pm on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can shorten and speed up that code a bit by using the [NC] flag to make the rule-pattern compare case-insensitive, avoiding two range checks per iteration:

RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{HTTP_HOST} ^(groups如ortal在oards妃y夙uide好ews)\.example\.com
RewriteRule ^([a-z0-9-]+/)?([a-z0-9-]+)?/? index.php?l=%1&s=$1&sub=$2 [NC,L,QSA]

Other changes for speed, disambiguation, style, and habit... For example, since back-references always refer to the last-matched RewriteCond subpatterns, I like to make the RewriteRule and the RewriteCond that it back-references contiguous. This prevents errors from being introduced if parentheses are later added to other intervening RewriteCond(s) for some reason.

Since REQUEST_URI will never contain anything but the URL-path --i.e. it will never contain the query string-- I removed the meaningless (in this case) ".*" on the end of "^/index\.php.*" and end-anchored it to to disambiguate the pattern so it matches one and only one URL-path --exactly "/index.php"-- and nothing else.

Jim