Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: Canonical Hostname, yes. Subdomain, NO!

         

sosoo

10:11 am on Dec 14, 2006 (gmt 0)

10+ Year Member



I'm tearing my hair out. This forum is full of good examples of how to use mod_rewrite but my attempt to cherry pick the bits I needed led to a mess.

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.

phranque

10:40 am on Dec 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if you post your examplified rewrite directives we could comment on your approach.
the forum moderator is a rewrite guru so you will eventually get this figured out.
it is probably a simple regular expression issue.

Alex_TJ

10:57 am on Dec 14, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi sosoo,

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

sosoo

11:06 am on Dec 14, 2006 (gmt 0)

10+ Year Member



Ok Alex_TJ, my code was pretty close to yours which I lifted from another post in the forum.

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.

Alex_TJ

11:22 am on Dec 14, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Why not carry this through all the way and maybe we'll help out some other people in the future as well.
Anyone have any insight as to how we can achieve the same thing for the subdomains?
If it's worth doing, it's worth doing properly.. :)

jdMorgan

1:53 pm on Dec 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The biggest challenge is not mod_rewrite or regular expressions. It is defining the problem and desired behaviour in a thorough and precise manner. Once that is done, implementations is usually not very difficult. This is exactly the same problem faced by programmers using any language; You can't code a robust solution if the problem is not fully understood.

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