SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: htaccess help

  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess help

    Hi guys!

    I've been looking at htaccess and how to use rewrite rules in order to make a sort of "fake subdomain" for users.

    so far i have:

    Code PHP:
    <IfModule mod_rewrite.c>
     
    	RewriteEngine On
    	RewriteBase /
     
    	RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteRule ([a-z0-9-]+)/? http://$1.domain.com [R=301,NC,L]
     
    	RewriteCond %{HTTP_HOST} ^([a-z]+)\.domain\.com$
    	RewriteRule ^$ index.php?user=%1
     
    </IfModule>

    this will take any /somthing and make it a subdomain and then pass the subdomain value in as a param to pick up.

    i want to be able to now use actual parameters, and pass them through normaly with the extra "user" param from the "subdomain"
    e.g.

    Code PHP:
    fred.domain.com/index.php?page=1&sort=up

    would give me in $_GET

    ['user'] = 'fred'
    ['page']= 1
    ['sort'] = up

    but for the life of me I cant figure out how to do this! as when i add any other params, I loose the user bit

    Any help? =)

    Also any helpful tutorials on htaccess would be nice! as all ones ive found haven't really explained what each bit does and why =\

    Thanks in advance

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Change the rule to

    Code:
    RewriteRule ^$ index.php?user=%1 [QSA]
    QSA stands for Query String Append, i.e., add the original $_GET to the new request.

    A few more pointers:

    1) Please get rid of <IfModule mod_rewrite.c> and </IfModule>, once you've established mod_rewrite works there is no need to ask the server over and over again for each and every request.

    2) RewriteBase is hardly ever needed, just remove it. If the site happens to fall apart when you do (unlikely), put it back.

    3) Just to make it more clear end the RewriteCond for the host with a $ so nothing can follow it, i.e.,

    Code:
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
    4) Exlude index.php from the last redirection, and then use .? instead of ^$ for the last RewriteRule

    5) Enclose the match for first rule in ^ and $ so it only fires if the complete URL matches; what you have now will also fire on partial match, which is not what you'd want (probably)

    All in all:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-z0-9-]+)/?$ http://$1.domain.com [R=301,NC,L]
    
    RewriteCond %{REQUEST_URI} !^/index\.php
    RewriteCond %{HTTP_HOST} ^([a-z]+)\.domain\.com$
    RewriteRule .? index.php?user=%1 [L,QSA]
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •