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:

<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://$ [R=301,NC,L]
	RewriteCond %{HTTP_HOST} ^([a-z]+)\\.domain\\.com$
	RewriteRule ^$ index.php?user=%1

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”

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

Change the rule to

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.,

RewriteCond %{HTTP_HOST} ^(www\\.)?domain\\.com$

  1. Exlude index.php from the last redirection, and then use .? instead of ^$ for the last RewriteRule

  2. 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:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\\.)?domain\\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ http://$ [R=301,NC,L]

RewriteCond %{REQUEST_URI} !^/index\\.php
RewriteCond %{HTTP_HOST} ^([a-z]+)\\.domain\\.com$
RewriteRule .? index.php?user=%1 [L,QSA]