SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2003
    Location
    switzerland
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite beginner question

    Hi,

    I've multiple domain names pointing to my server (same IP for all names). So I want to do a redirection "per host",
    like www.host.com to /host/

    Can somone tell me, why this does not work:

    /.htaccess
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    # Rewrite Rule for host.com
    RewriteCond %{HTTP_HOST} ^(.*)\.host\.com$
    RewriteRule ^(.*)$ host/$1

    while these works:

    /.htaccess
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    # Rewrite Rule for host.com
    RewriteCond %{HTTP_HOST} ^(.*)\.host\.com$
    RewriteCond %{REQUEST_URI} !host
    RewriteRule ^(.*)$ host/$1

    why can't i delete the line
    RewriteCond %{REQUEST_URI} !host??

    Thank you for any hint!
    Greetings mirk

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mirk,

    >> (1) RewriteCond %{HTTP_HOST} ^(.*)\.host\.com$
    >> (2) RewriteRule ^(.*)$ host/$1
    >> Can somone tell me, why this does not work:

    request for host.com/pippo.gif
    (1) matches because http_host is host.com
    (2) rewrite the request as host/pippo.gif

    request for host.com/host/pippo.gif
    (1) matches because http_host is host.com
    (2) rewrite the request as host/host/pippo.gif

    request for host.com/host/host/pippo.gif
    (1) matches because http_host is host.com
    (2) rewrite the request as host/host/host/pippo.gif

    ...ad infinitum...

    >> (1) RewriteCond %{HTTP_HOST} ^(.*)\.host\.com$
    >> (2) RewriteCond %{REQUEST_URI} !host
    >> (3) RewriteRule ^(.*)$ host/$1
    >> while these works:

    request for host.com/pippo.gif
    (1) matches because http_host is host.com
    (2) matches because the request uri does not contain host (it's /pippo.gif)
    (3) rewrite the request to host/pippo.gif

    request for host.com/host/pippo.gif
    (1) matches because http_host is host.com
    (2) does not match because the request uri contains host
    exit :-)

    >> why can't i delete the line
    >> RewriteCond %{REQUEST_URI} !host??
    See the above explanation.

    Hope it was clear...
    Basically the rewrite parser when it will match a rewriterule it will make a new internal request.

    I have logged the rewrite steps into a log file.
    I used inside my httpd.conf
    RewriteLogLevel 9
    RewriteLog "c:\mod_rewrite.c"
    The I created an .htaccess with your rules (the former ) and here the ad-infinitum result:
    Code:
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/pippo -> pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^(.*)$' to uri 'pippo'
    RewriteCond: input='www.mytest.net' pattern='^(.*)\.mytest.net$' => matched
    [per-dir c:/usr/www/mytest.net/htdocs/] rewrite pippo -> host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] add per-dir prefix: host/pippo -> c:/usr/www/mytest.net/htdocs/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip document_root prefix: c:/usr/www/mytest.net/htdocs/host/pippo -> /host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] internal redirect with /host/pippo [INTERNAL REDIRECT]
    [per-dir c:/usr/www/mytest.net/htdocs/] add path-info postfix: c:/usr/www/mytest.net/htdocs/host -> c:/usr/www/mytest.net/htdocs/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/host/pippo -> host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^(.*)$' to uri 'host/pippo'
    RewriteCond: input='www.mytest.net' pattern='^(.*)\.mytest.net$' => matched
    [per-dir c:/usr/www/mytest.net/htdocs/] rewrite host/pippo -> host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] add per-dir prefix: host/host/pippo -> c:/usr/www/mytest.net/htdocs/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip document_root prefix: c:/usr/www/mytest.net/htdocs/host/host/pippo -> /host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] internal redirect with /host/host/pippo [INTERNAL REDIRECT]
    [per-dir c:/usr/www/mytest.net/htdocs/] add path-info postfix: c:/usr/www/mytest.net/htdocs/host -> c:/usr/www/mytest.net/htdocs/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/host/host/pippo -> host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^(.*)$' to uri 'host/host/pippo'
    RewriteCond: input='www.mytest.net' pattern='^(.*)\.mytest.net$' => matched
    [per-dir c:/usr/www/mytest.net/htdocs/] rewrite host/host/pippo -> host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] add per-dir prefix: host/host/host/pippo -> c:/usr/www/mytest.net/htdocs/host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip document_root prefix: c:/usr/www/mytest.net/htdocs/host/host/host/pippo -> /host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] internal redirect with /host/host/host/pippo [INTERNAL REDIRECT]
    [per-dir c:/usr/www/mytest.net/htdocs/] add path-info postfix: c:/usr/www/mytest.net/htdocs/host -> c:/usr/www/mytest.net/htdocs/host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/host/host/host/pippo -> host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^(.*)$' to uri 'host/host/host/pippo'
    RewriteCond: input='www.mytest.net' pattern='^(.*)\.mytest.net$' => matched
    [per-dir c:/usr/www/mytest.net/htdocs/] rewrite host/host/host/pippo -> host/host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] add per-dir prefix: host/host/host/host/pippo -> c:/usr/www/mytest.net/htdocs/host/host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] strip document_root prefix: c:/usr/www/mytest.net/htdocs/host/host/host/host/pippo -> /host/host/host/host/pippo
    [per-dir c:/usr/www/mytest.net/htdocs/] internal redirect with /host/host/host/host/pippo [INTERNAL REDIRECT]
    Okay,
    because know we understood what happens...it's time to make some optimizations to the rules:

    Code:
    RewriteEngine On
    # Handles all kind of host and sub-host such as:
    # host.com www.host.com subhost.com www.sub.sub.host.com
    RewriteCond %{HTTP_HOST} ^(.+\.)?host\.com$ [NC]
    # If the request DOES NOT start with host then make
    # the internal redirection
    # The below trick will let us with a single line
    # to avoid the ad infinitum loop
    RewriteRule !^host host%{REQUEST_URI} [L]

    :-) pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


Bookmarks

Posting Permissions

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