Results 1 to 8 of 8
  1. #1
    Regular Member
    Join Date
    Aug 2006
    Posts
    68

    More mod rewrite shenanigans

    I have something I am wrestling with for a client, whereby the root of a domain is redirected to a folder, but how do I put in exceptions to mod rewrite so that not everything is rewritten?

    For example (using made up domains):

    We have a network of sites on allthesites.com, eg thesite1.com, thesite2.com, etc dynamically served up.

    User types in www.thesite1.com/customerpage and is rewritten to http://www.allthesites.com/homepages.../customerpage/

    But if they just type in www.thesite1.com they should not be redirected, otherwise they won't get to index.php which is in the root of allthesites.com! Is there a way to tell it not to redirect certain files/folders (ie images, cms etc so the site still works!)

    If anyone wants more specific examples to see it actually (using real domains) in action on the old version, PM me.

  2. #2
    Munky! MrBen's Avatar
    Join Date
    Sep 2003
    Location
    nr Woking, England
    Posts
    2,646
    You will want to use RewriteCond. There's a whole host of variables you can get at but the following will probably be the most useful for what you're trying to do I think...

    HTTP_HOST - In the example below, if the user is visiting via www.domain1.tld OR domain1.tld OR domain2.tld, they will be redirected to www.domain2.tld.

    Code:
    RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tld$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^domain2\.tld$ [NC]
    RewriteRule ^(.*)$ http://www.domain2.tld/$1 [R=301,L]
    REQUEST_URI and QUERY_STRING - In the example below, if the user is requesting the page /htmlsite/page.asp AND has a query string of specifically docid=1 they will be redirected to http://www.domain.tld/new-page.htm?foo=bar

    Code:
    RewriteCond %{REQUEST_URI} ^/htmlsite/page.asp$ [NC]
    RewriteCond %{QUERY_STRING} ^docid=1$ [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/new-page.htm?foo=bar [R=301,L]
    You can obviously combine these examples and have as many conditions as you like.

    Ben
    Veterinary Practice Management System by SoftFooding
    Internet Data Usage Calculator: Estimate your monthly bandwidth usage for your Internet connection.

  3. #3
    Munky! MrBen's Avatar
    Join Date
    Sep 2003
    Location
    nr Woking, England
    Posts
    2,646
    I should add you do NOTs as well. For example, to say if the request URI doesn't include /folder/ you would write,

    RewriteCond %{REQUEST_URI} !/folder/

    Ben
    Veterinary Practice Management System by SoftFooding
    Internet Data Usage Calculator: Estimate your monthly bandwidth usage for your Internet connection.

  4. #4
    Regular Member
    Join Date
    Aug 2006
    Posts
    68
    Thanks MrBen, I suppose I should get the rewrite working first before I worry about the other conditions.

    A standard rewrite in .htaccess works:
    redirect 301 /test/ http://www.domain.tld/homepages/www.domain.tld/test/

    So what I need to do is get a mod rewrite version that is a little more flexible as I can't really write one of those for every page!

    Basically what I am trying to get working first is (condition free at the moment):

    RewriteRule www.$domain.$tld/$anypath /homepages/www.$domain.$tld/$anypath

    Based on my limited knowledge of it so far I had:
    RewriteRule ^www\.(.*)\.(.*)/(.*) /homepages/www\.$1\.$2/$3

    Which needless to say did not work.

    Anyone know a good site that explains all the *.^% stuff - is that to do with regular expressions?

    EDIT:
    I got this to work, which is a step in the right direction but the domain is manually put in so it will only work for one of the 8 or so domains:


    RewriteCond %{REQUEST_URI} !/homepages/
    RewriteRule ^(.*)/?$ homepages/www.domain.ltd/$1/

    Think I will have another bash after lunch!
    Last edited by RoryB; 20th June 2007 at 02:54 PM. Reason: New version removes trailing slash probs

  5. #5
    Munky! MrBen's Avatar
    Join Date
    Sep 2003
    Location
    nr Woking, England
    Posts
    2,646
    Ah, I thought you already had part of the rewrite working. Something to think about is what if the visitor uses just domain.tld rather than www.domain.tld? As it stands the rewrite will fall over because the folder is called www.domain.tld. You have two choices for that, either add a symlink or have an initial 301 redirect that forces the user to a particular version of the URL (which is what I've done in the example below - one required for each domain).

    Code:
    ## Force visitor to use www version of domain
    RewriteCond %{HTTP_HOST} ^domain1\.co\.uk$ [NC]
    RewriteRule ^(.*)$ http://www.domain1.co.uk/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^domain2\.co\.uk$ [NC]
    RewriteRule ^(.*)$ http://www.domain2.co.uk/$1 [R=301,L]
    
    ## Rewrite to domain folder
    RewriteCond %{HTTP_HOST} !^(www\.)?alldomains\.co\.uk$ [NC]
    RewriteCond %{REQUEST_URI} !/homepages/ [NC]
    RewriteCond %{REQUEST_URI} !/images/ [NC]
    RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
    RewriteCond %{REQUEST_URI} !^/$ [NC]
    RewriteRule ^(.*)$ /homepages/%{HTTP_HOST}/$1 [L]
    The rewrite above will kick in for all requests unless they user used the domain www.alldomains.co.uk or alldomains.co.uk. Also, requests to the homepages folder, images (in the root), the default/root file (i.e. domain1.co.uk or domain2.co.uk etc) and index.php (assuming that is the name of your default file in the root) are not rewritten. Everything else is rewritten to /homepages/www.domain1.co.uk/ or /homepages/www.domain2.co.uk/ etc

    Ben
    Veterinary Practice Management System by SoftFooding
    Internet Data Usage Calculator: Estimate your monthly bandwidth usage for your Internet connection.

  6. #6
    Regular Member
    Join Date
    Aug 2006
    Posts
    68
    Thanks, I will look at adding that in to catch www. droppers.

    This seems to work for me, although it seems to be code 301 redirecting rather than stealthily 200 displaying but that may just be my browser cache needing to be cleared:

    Code:
    RewriteRule ^(.*)/?$ homepages/%{HTTP_HOST}/$1/
    I had to put in a few conditions (thanks again MrBen) to stop it misplacing resources and our admin section but it seems to be working ok now.

    EDIT: ok I take that back still need a few exceptions put in!
    Last edited by RoryB; 20th June 2007 at 03:15 PM.

  7. #7
    Regular Member
    Join Date
    Aug 2006
    Posts
    68
    The only thing is I had to put in rewrite conditions for all the maindir files other than index.php search, listing, map etc. Is there a quicker way, say to get it to ignore any files ending .php? All the files in the redirected homepages directories are html so nobody should be calling a php file to go there anyway!

    EDIT:

    AARGH the battle goes on.

    So it seems that it works for the main domain, ie thesite1.com however thesite2.com is behaving strangely, I think that there is some issue with file paths that seems to occur for one but not the other. This makes no sense to me seeing as they both run dynamically from one piece of code! Could it be something to do with thesite2.com being an alias?

    Looks like I will have to investigate further. I wish the main developer wasn't in the USA till October!
    Last edited by RoryB; 20th June 2007 at 05:20 PM. Reason: Oh no it doesn't

  8. #8
    Munky! MrBen's Avatar
    Join Date
    Sep 2003
    Location
    nr Woking, England
    Posts
    2,646
    If it's only HTML files then you should be able to use the following,

    Code:
    ## Rewrite to domain folder
    RewriteCond %{HTTP_HOST} !^(www\.)?alldomains\.co\.uk$ [NC]
    RewriteCond %{REQUEST_URI} !/homepages/ [NC]
    RewriteCond %{REQUEST_URI} !/images/ [NC]
    RewriteCond %{REQUEST_URI} ^/(.*)\.htm$ [NC]
    RewriteRule ^(.*)$ /homepages/%{HTTP_HOST}/$1 [L]
    The above only rewrites files specifically ending in .htm.

    If you want to be more specific, feel free to drop me a PM as I've tested what I'm posting and it works fine for me - but I might be understanding what you're trying to achieve.

    Ben
    Veterinary Practice Management System by SoftFooding
    Internet Data Usage Calculator: Estimate your monthly bandwidth usage for your Internet connection.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •