littleRoom
littleRoom

Reputation: 81

.htaccess redirect adding a hash while keeping the query string

Using mod-rewrite in a .htaccess how do you add a hash to a url while keeping the query string in front of it? Everything i have tried appends the query string to the end, which then makes it part of the hash.

This is what i'm trying to do:

http://example.com/order_now?utm_campaign=eblast082814

redirected to

http://example.com/index.php?utm_campaign=eblast082814#order_now

Here is my current non-working .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)?$ /index.php?#$1 [QSA,R,NE,L]

Above is wrongly redirecting to

http://example.com/index.php?#order_now&utm_campaign=eblast082814

Upvotes: 2

Views: 769

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

Seems like query string is added right after #order_now by default.

Here's a solution (by capturing query string and add it manually)

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/\.]+)?$ /index.php?%1#$1 [R,NE,L]

Upvotes: 2

Related Questions