Thread: Mod Rewrite

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    5
    Rep Power
    0

    Mod Rewrite


    does anyone know why this doesn't work:

    RewriteCond %[HTTP_HOST} ^w{3}\.([^.]+)\.example\.com$ [NC] and RewriteRule ^(.*)$ http://$1\.example\.com [L,R=301]

    I want to redirect www.subdomain.example.com to subdomain.example.com (minus www), however, I get 400 bad request errors when doing this

    But when I add this:

    ServerAlias *.example.com

    Then i don't get any more 400 errors.
    but then www.subdomain.example.com/page.html will redirect to page.html.example.com

    And, with serveralias *.example.com enabled, my main domain doesn't work anymore (the rules probably get applied to it too)

    <VirtualHost xx.xx.xx.xx>
    BytesLog domlogs/host.example.com-bytes_log
    ServerName host.example.com
    #ServerAlias *.example.com
    DocumentRoot /usr/local/apache/htdocs

    RewriteEngine on
    RewriteLog "/usr/local/apache/logs/rewrite_log"

    Options -ExecCGI -Includes
    RewriteCond %{HTTP_HOST} ^w{3}\.([^.]+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://$1\.example\.com [L,R=301]

    RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ ## i thought these rules would protect my main domain?
    RewriteCond %{HTTP_HOST} !^host\.example\.com$

    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]

    ...
    NORMAL REWRITE RULES

    Thanks in advance
  2. #2
  3. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    does anyone know why this doesn't work:
    Code:
    RewriteCond %[HTTP_HOST} ^w{3}\.([^.]+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://$1\.example\.com [L,R=301]
    I want to redirect www.subdomain.example.com to subdomain.example.com (minus www), however, I get 400 bad request errors when doing this
    I'm stopping here b/c there's a large error. Fix this one first and let me know if it solves your problem. If not, then I'll read the rest.

    Also, please use &#91;code][/code] tags around your rule sets (see how I change it in the quote above?).

    In your RewriteRule, you use $1. $1 represents what you previously matched in the RewriteRule with ^(.*)$, which is not what you want, or rather, you probably want that at the end of the rewritten request. %1 represents what was matched in the previous RewriteCond. And you only escape . in patterns. So what you want is this:
    Code:
    RewriteCond %{HTTP_HOST} ^w{3}\.([^.]+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://%1.example.com$1 [R=301,L]
    However, a more portable version would be like this:
    Code:
    RewriteCond %{HTTP_HOST} ^w{3}\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1$1 [R,L]
    Note that if you move outside of the VirtualHost context, you'll need to use /$1 instead of $1 (and might anyway!).
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    5
    Rep Power
    0
    Hi,

    Thanks for your help and sorry for my late reply.

    Could you please tell me if fixing this error would stop the rules from being applied to www.example.com (the main domain)?

    I'm anxious to try it but the website is live and I wouldn't want anyone to see errors like that.

    Also, does ServerAlias *.example.com need to be present or not?

    Thanks in advance
  6. #4
  7. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    My first ruleset should not affect www.example.com, but my second one will. But neither are guaranteed to work w/o problems or tweaking.
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.

IMN logo majestic logo threadwatch logo seochat tools logo