-1

I have some wildcard redirects which are currently working fine...

https://www.domain-name.com/$1

However, I would like to add a GET variable after it, so that in my CMS, I can trace the referrer. I have tried the below, however it just outputs the $1 to the URL.

https://www.domain-name.com/$1?redirect=foobar

Hopefully this is a simple fix!

Edit - these redirects are generated from cPanel, so the generated code is...

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "https\:\/\/www\.domain\.uk\/\$1\?redirect\=redirect" [R=301,L]
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38

1 Answers1

1

There is no wildcard redirect here. It just redirects the home page to literally /$1?redirect=redirect. Also, there's no point in escaping (\) any character of the target URL.

In order to redirect each request to itself with a query string appended

# prevent a redirect loop
RewriteCond %{QUERY_STRING} !redirect=
# append query string
RewriteRule ^(.*)$ https://www.domain.uk/$1?redirect=foobar [R,L]

If you want to preserve any existing query string, add the QSA flag, e.g. R,L,QSA

When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198