Moldevort
Moldevort

Reputation: 193

Redirect with optional query string (.htaccess)

I have coded a small PHP-Script, with which I have something like a short url.

short.url/string

gets directed to

short.url/redirect.php?id=string

Code:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.+)$ ./redirect.php?id=$1

Some of these short URLs have query strings; unfortunately, I have difficulty combining query strings with the code above

short.url/string?query

should get directed to

short.url/redirect.php?id=string&q=query

How could I do that?

Upvotes: 1

Views: 143

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You need to use QSA (Query String Append) flag in your rule

QSA flag is used to combine both new and old query strings.

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.+)$ ./redirect.php?id=$1 [QSA]

Upvotes: 1

Related Questions