0

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

Hammad Khan
  • 14,008
  • 14
  • 100
  • 123
  • You are using `℅n` variable but not setting it. `℅n` refers to url part matched in `RewriteCond` . See the mode rewrite manual http://httpd.apache.org/docs/current/mod/mod_rewrite.html – Amit Verma Feb 07 '18 at 15:45
  • @starkeen I have explained my problem well. The dup that you marked, does not address my question. The problem in my case is to add string `at the end of entire url` that includes get parameters as well. The question should be opened to be answered. – Hammad Khan Feb 07 '18 at 16:14
  • The problem is not with adding sting at the end of redirected url your problem is that you are not using correct variables and backrefrences and not following the manual. – Amit Verma Feb 08 '18 at 10:30
  • @starkeen So someone can fix it, If I dont know about it. But the question has been closed. – Hammad Khan Feb 10 '18 at 04:56
  • Your question has been reopened. – Amit Verma Feb 10 '18 at 08:33

1 Answers1

1

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.

Amit Verma
  • 38,175
  • 19
  • 80
  • 104