views:

105

answers:

1

I hate asking questions about mod_rewrite, but I can't seem to get these rules working properly. I had it setup and figured out once before, but a few years have passed and now it's just not playing nice and my mod_rewrite knowledge is back to uhhhh......

Essentially what I want to do is force www on the main domain, example.com. (or forcing no www would be even better, but I haven't remotely been able to get that working properly along with dynamic subdomains).

If my.example.com rules exist, then follow them accordingly

For anything else, ie: *.example.com, pass to another file to parse.

The rules I have currently handles forcing the www, and properly handles the dynamic subdomain, but completely ignores my.example.com rules, handling it as a wildcard subdomain. It seems I can get any one of my rules working properly, but when trying to get them working together it's just being a bastard. If anyone knows have to get this working properly it would be super appreciated.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# my.example.com  defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1  [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]

# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
A: 

Try these rules:

# remove www on main
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# my.example.com  defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1  [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]

# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]

The first rule will now redirect the +www.example.com* to example.com. And the third rule will catch any request to a non existing host.

Gumbo
That seems to be working perfectly. After Tim's comment I cleared my cache again and my original rules started working, so as usual it was mostly a user error on my part, but I'm happy to have the no-www working, so much appreciated Gumbo!
Jervis