TheTechGuy
TheTechGuy

Reputation: 17364

add string at the end of URL using htaccess which has query string also

I want to change

domain.com/division1/index.php?members/maxmusterman.5

to

domain.com/division1/index.php?members/maxmusterman.5/#div

That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this

RewriteEngine On
RewriteCond %{REQUEST_URI}  index.php?
RewriteRule (.*)  /%1/%{QUERY_STRING}&123 [L,QSA,R=301]

but it returns

domain.com/members/maxmusterman.5&123?members/maxmusterman.5

Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div

Upvotes: 0

Views: 489

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

To redirect

domain.com/division1/index.php?members/maxmusterman.5

to domain.com/division1/index.php?members/maxmusterman.5/#div . You can use something like the following :

RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]

I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.

NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .

solution #2

RewriteEngine on

RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]

Clear your browser cache before testing these redirects.

Upvotes: 1

Related Questions