DaveHolt
DaveHolt

Reputation: 129

.htaccess rewriting with multiple $_GET variables

I'm really struggling to write my .htaccess file properly. I would like the following to happen:

  1. x.domain.com --> redirect to --> domain.com/index.php?id=x
  2. If there is a $_GET variable on the original URL, transfer this to the second URL: x.domain.com/?y --> redirect to --> domain.com/index.php?id=x&y

So far, I've managed to get as far as mentioned in the 1st point.

But can't work out where to go next to achieve the 2nd point.

Do I need to write new lines for a separate rule, or amend the current lines?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$
RewriteRule .* http://www.domain.com/index.php?id=%1 [L,P]

Any help appreciated.

Upvotes: 1

Views: 163

Answers (1)

deceze
deceze

Reputation: 522016

...
RewriteRule .* http://www.domain.com/index.php?id=%1 [L,P,QSA]

You're looking for the QSA (query string append) flag, which appends the query string back to the rewritten URL.

Upvotes: 2

Related Questions