Forum Moderators: phranque
I'm trying to rewrite http://example.com to http://www.example.com
but it's matching ALL subdomains and I didn't want that.
I don't want to mess with subdomains at all, but they are getting redirected to http://www.example.com/subdomain
I know the solution is simple but I don't understand the syntax well enough yet.
I had exactly the same problem a few months ago and here is how I solved it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.net [NC]
RewriteRule ^(.*)$ [site.net...] [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm
RewriteRule ^index\.htm$ [site.net...] [R=301,L]
The only but is that I wanted to rewrite also the subdomain.site.net to +www as well but I could never figure out how to do that without messing up the rewrite on the main site.
This will work for your main site and not mess up the subdomains, doing the same thing also for the subdomains I'm going to have to leave to someone else...any ideas are obviously very welcome (I know technically a subdomain shouldn't be www, but visitors expect it).
Take care
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.co.uk [NC]
RewriteRule ^(.*)$ [site.co.uk...] [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ [site.co.uk...] [R=301,L]
[edit]
...and it's all working now which is great. My site is no longer broken so I can get down to learning what all this stuff actually does. Regular expressions are not my strong point, and I don't want to complicate things by rewriting index.html and/or index.htm for subdomains too.
Anyway, Thanks very much.
There is admittedly a learning curve, in that the problem/solution must be defined in terms of server variables and conditions that mod_rewrite can test and affect -- We can't do the impossible, of course. But with experience (or a lot of reading on this and other forums), the problem is not insurmountable.
All that said, what does "the same thing" mean in this sentence?
> Anyone have any insight as to how we can achieve the same thing for the subdomains?
Jim