Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule doesn't work right.

Rewrite static URLs in subdomains to a script

         

miyagidk

1:06 pm on Dec 9, 2007 (gmt 0)

10+ Year Member



Hi how can i get this to work within the "subdomains"?

rewriteEngine on

RewriteRule ^([a-zA-Z0-9/?+]+)$ index.php?post=$1 [nc]

rewriteBase /

#### URL rewrite Handler for subdomains (by Randall Krause) ####

rewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
rewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
rewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
rewriteRule ^(.*) subdomains/%1/$1 [E=SUBDOMAIN:%1,L]
rewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

/Chris.

jdMorgan

8:43 pm on Dec 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this, perhaps:

RewriteBase /
#
# Get requested subdomain to "SdPath"
RewriteCond %{ENV:SubdRW} !^Done$
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule !^subdomains/ - [E=SdPath:%1]
#
# Rewrite certain requests to index.php script in subdomain
RewriteCond %{ENV:SubdRW} !^Done$
RewriteRule ^([a-z0-9/]+)$ /subdomains/%{ENV:SdPath}/index.php?post=$1 [NC,E=SubdRW:Done,L]
#
# Rewrite all others to subdomain as-is
RewriteCond %{ENV:SubdRW} !^Done$
RewriteRule (.*) /subdomains/%{ENV:SdPath}/$1 [E=SubdRW:Done,L]

The variable "SubdRW" is used to prevent recursion in this .htaccess code, similar to the variables in the code you posted.

I removed "A-Z" from your pattern group, because it's redundant if you use the [NC] flag (No Case).
I also removed "?" and "+" because although they may appear in query strings, these characters will not appear in the URL-path examined by RewriteRule.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 10:54 pm (utc) on Dec. 10, 2007]

miyagidk

11:31 am on Dec 10, 2007 (gmt 0)

10+ Year Member



Hi Jim,
thanks for your replay.

Im getting a Internal Server Error with the rules in the .htacces ant i cant find the error in the rules :(

.htaccess: RewriteCond: bad flag delimiters

Can you help me out?

/Chris.

jdMorgan

10:57 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This was caused by my typo in the flags of the first RewriteRule -- I had typed "%{E=SdPath:%1}" instead of the correct "[E=SdPath:%1]"

I corrected it in the code above so no-one else who copies it will have the same problem.

Jim

miyagidk

2:04 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Hi again,
sorry to keep coming back ;)
But my knowledge about mod_rewrite is absolutely on beginners stadium!

it looks like its looping now.

[Tue Dec 11 02:59:30 2007] [debug] core.c(3027): [client x] r->uri = /subdomains//subdomains/subdomains/subdomains/subdomains/subdomains/subdomai
ns/subdomains/subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/subdomains/subdomains/sub
domains/subdomains/subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/subdomains/subdomains/sub
domains/subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/subdomains/subdomains/sub
domains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/subdomains/subdomains/ind
ex.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//subdomains/index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//index.php
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /subdomains//
[Tue Dec 11 02:59:30 2007] [debug] core.c(3033): [client x] redirected from r->uri = /

Hope u can help again.

/Chris.

jdMorgan

2:43 am on Dec 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the fastest solution is to use a brute-force and somewhat redundant, but simpler method:

RewriteBase /
#
# Rewrite certain requests to index.php script in subdomain
RewriteCond $1 !^subdomains/
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule ^([a-z0-9/]+)$ /subdomains/%1/index.php?post=$1 [NC,L]
#
# Rewrite all others to subdomain as-is
RewriteCond $1 !^subdomains/
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]

Jim

[edited by: jdMorgan at 2:44 am (utc) on Dec. 11, 2007]