SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Salt Spring
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trailing slash for "alias" domains in mod_rewrite

    Hello,
    I would appreciate it if someone could help me out.
    Using the following directives in my root ataccess file, I have set up xyz.com as a domain alias, (a subdirectory) to the original abc.com domain so that people entering the URL abc.com will see one site and people entering the URL xyz.com while see aother different site, while keeping the URL the same as the user typed it:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} xyz.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/xyz/.*$
    RewriteRule ^(.*)$ /xyz/$1 [L]

    #where xyz directory (virtual site) is located in abc.com/xyz/

    It works; however if the "alias" url xyz.com does not include a trailing foward slash after a directory folder - e.g., xyz.com/somefolder versus xyz.com/somefolder/ , the browser automatically adds the forward slash, refreshes the URL and changes it to the absolute path in the browser - abc.com/~xyz/somefolder/

    Is there someway through mod_rewrite to add a foward slash to an "alias" url without it defaulting to the "real" url in the browser?

    The following rules add foward slashes but they also do an external redirect which causes the url to default to the "real" url:

    RewriteRule ^/somepath/somedir$ /somepath/somedir/ [R]
    Or,
    RewriteBase /~xyz
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+[^/])$ $1/ [R]


    Thanks, Sam

  2. #2
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Salt Spring
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again,
    I figured it out on my own. Below is my latest simple code.

    The first line, excludes any url requests for my parked "alias" domains with "webmail" - e.g, xyz.com/webmail. Webmail is an online email service offered by the hosting company where I am hosting my abc.com site.

    RewriteCond %{HTTP_HOST} !^.+\.+/webmail$ [NC]

    The second set of three lines are a futher refinement from what I had in my previous email. If the user enters xyz.com then it stays xyz.com. If she enters www.xyz.com then it stays www.xyz.com.

    RewriteCond %{HTTP_HOST} ^(www\.)?xyz.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/~xyz/.*$
    RewriteRule ^(.*)$ http://%1xyz.com/~xyz/$1

    The last set of three lines deals with the auto redirect to correct a trailing slash, and to retain the initial xyz.com domain url - in other words, to prevent the URL from defaulting to the aboslute URL path of my real, parent domain (abc.com). It also directs the user from the abc.com URL to the alias xyz.com URL, if he enters abc.com/~xyz or abc.com/~xyz with or without the slash.

    RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
    Rewriterule ^~dr/(.*)$ http://%1xyz.com/$1 [R]
    Rewriterule ^~dr$ http://%1xyz.com [R, L]

    The code works fine. However, if a missing trailing slash redirect happens, "www." is automatically added as a prefix. This happens before the last set of lines spring into action. If I could refer back to the %1 string in the second set of directives, I could correct it to make the url redirects, with or without "www.", entirely consistent. Does anyone have any ideas?

    Regards,
    Sam


    All together the code is:

    RewriteCond %{HTTP_HOST} !^.+\.+/webmail$ [NC]

    RewriteCond %{HTTP_HOST} ^(www\.)?xyz.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/~xyz/.*$
    RewriteRule ^(.*)$ http://%1xyz.com/~xyz/$1

    RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
    Rewriterule ^~dr/(.*)$ http://%1xyz.com/$1 [R]
    Rewriterule ^~dr$ http://%1xyz.com [R, L]

    ------------
    For additional "faux" or "alias" domains (mno) under abc.com:

    RewriteCond %{HTTP_HOST} ^(www\.)?mno.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/~mno/.*$
    RewriteRule ^(.*)$ http://%1xyz2.com/~mno/$1

    RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
    Rewriterule ^~dr/(.*)$ http://%1mno.com/$1 [R]
    Rewriterule ^~dr$ http://%1mno.com [R, L]

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Salt Spring
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Opps a typo in my last "faux" example - the "xyy2" should have been "mno" . The code correct code is:

    RewriteCond %{HTTP_HOST} ^(www\.)?mno.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/~mno/.*$
    RewriteRule ^(.*)$ http://%1mno.com/~mno/$1

  4. #4
    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
    when you request an URI that's a directory but without the trailing slash then mod_dir.c, an Apache module, will force the broswer to make an external redirection, 30x, to the new URI containing the trailing slash.

    Based on your mod_rewrite aliases and suppose the main server is called example.net, and you asked for example.com/directory_name mod_dir has two choices:
    redirecting to example.net/directory_name/
    redirecting to example.com/directory_name/

    uh ?
    yes, it depends on how UseCanonicalName was set.
    In fact the external redirection can be based on the SERVER_NAME (example.net) or based on the HTTP_HOST (example.com).
    In your case, and that's the default value, mod_dir (in reality the C routine that is responsible to create the URI) will use the SERVER_NAME.

    That said, with mod_rewrite you can avoid that by using this:

    The scenario is this:
    example.net it is the main domain, in pratice it is what contains SERVER_NAME
    example.com and example.org are two alias domains

    example.localhost is a non main domain that you don't want to do aliasing with mod_rewrite

    /.htaccess
    Code:
    RewriteEngine On
    
    #
    # example.local and example.net won't be parsed
    #
    RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.(local|net)$ [NC]
    RewriteRule ^.*$ - [L]
    
    #
    # example.com alias domain
    #
    # Check that the request was 'bout example.com
    RewriteCond %{HTTP_HOST} !^([^.]+\.)?example\.com$ [NC,OR]
    # To avoid internal loops
    RewriteCond %{REQUEST_URI} ^/example\.com/.*$ [NC]
    # S=2 means SKIPS the next two RewriteRule
    # if the previous RewriteCond matched
    RewriteRule ^.*$ - [S=2]
    
    # If the URI ends with a trailing slash
    # okay make the internal redirection
    RewriteRule ^(.+/)?$ /example.com/$1 [L]
    
    # The URI doesn't end with a trailing slash
    # and the URI is a valid directory name
    RewriteCond %{DOCUMENT_ROOT}/example\.com%{REQUEST_URI} -d
    # FORCE the 30x, in pratice we are doing the mod_dir job :-)
    RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
    
    #
    # example.org alias domain
    #
    RewriteCond %{HTTP_HOST} !^([^.]+\.)?example\.org$ [NC,OR]
    RewriteCond %{REQUEST_URI} ^/example\.org/.*$ [NC]
    RewriteRule ^.*$ - [S=2]
    
    RewriteRule ^(.+/)?$ /example.org/$1 [L]
    
    RewriteCond %{DOCUMENT_ROOT}/example\.org%{REQUEST_URI} -d
    RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]




    p.s.
    In my examples I supposed to have
    /example.com/etc
    /example.org/etc
    as my directory layout.

    You can change it obviously...
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  5. #5
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Salt Spring
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI Pippo,
    I greatly appreciate your elegant coding. I learnt a lot from it. Thanks! However, I wasn't able to get it to work in my particular situation. I get a "Cannot find server or DNS Error". I won't have time to really experiment with your code for a little while.

    I don't have access to any system config. files. I am serving my sites through a small virtual hosting plan. Otherwise, I would have addressed the trailing slash issue through the "usecanonicalname" directive. I get 20 parked domains (internally pointing to the homepage of my main domain) and five add-on domains as part of my hosting package. Through mod-rewrite, I am attempting to transform my parked domains into their own unique alias domains, allowing me to host up to 25 domains.

    Do I have a "SERVER_NAME" if I am on a vitural hosting plan, or just assigned HTTP-HOSTs (my main account domain and the other five-add ons)?

    In the last line of your code:

    RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
    Isn't it missing some reference to the "example.org" folder/directory. Won't it externally redirect to "http://example.org/requested url" when it should it should be refering to "http://example.org/example.org/example.org/requested url in its redirect, while displaying "http://example.org/requested url" in the browser.

    My pea brain is obviously missing something here.

    Thanks again,
    Sam

  6. #6
    SitePoint Addict adam2003w's Avatar
    Join Date
    Mar 2004
    Location
    colorado
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for this discussion. I didn't post the question, but I am also learning the mod_rewrite .htaccess


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
  •