12

My aim is this URL:

component/users/?view=registration

To:

registration.html

the .htaccess is in the folder mysite of the website.

i tried this:

RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]

But i doesnt work...

When i try this:

RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]

it works very well.

So how can i fix this problem with the question mark. I already read thats it is not a part of the URL (its appended). I have read that i have to use something like querystring, but i didn't really understand the syntax.

Maybe someone could write the solution of this problem? Would be awesome =)

robsch
  • 9,358
  • 9
  • 63
  • 104
Bardock
  • 237
  • 2
  • 4
  • 9

2 Answers2

31

You need to use %{QUERY_STRING} to capture the query string data:

RewriteCond %{QUERY_STRING} ^view=(.*)$
RewriteRule ^component/users/?$ %1.html? [R=301,L]

The above rule/condition will take the value of the query string view and use it to form your redirect if the path component/users matches.

Prix
  • 19,417
  • 15
  • 73
  • 132
  • 1
    thanks for the answer but it doesnt work. this is what i get: http://localhost/mysite/registration.html?view=registration when i type: http://localhost/mysite/component/users/?view=registration do you have an idea how this "?view=registration" can be removed? – Bardock Sep 01 '13 at 11:51
  • 1
    @Bardock Please tell me how can its remove the "?view=registration" ? – AnhTN Apr 26 '18 at 08:02
  • i bet you're using `$1` instead of `%1`. Look carefully at the answer, it says to use `%1` which will match the correct thing. I'm not sure why, but in my case $1 matches the whole rule as well... – anarcat May 04 '23 at 20:12
  • @anarcat $ is used to capture the left hand of the RewriteRule while % is used to capture the parameters coming from the RewriteCond https://httpd.apache.org/docs/trunk/rewrite/intro.html – Prix May 05 '23 at 00:06
  • yeah, i know that, but there's no group on the left side! – anarcat May 06 '23 at 14:11
-3
RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]

You need a \ because ? is part of a regular expression. To Use it as a string u need to escape it with a \

Linktip: http://ole.michelsen.dk/tools/regex.html

Mainz007
  • 533
  • 5
  • 16