Good evening. Friends, help solve the problem: I want to alter the following URLs:

http://site.com/user?id=1 

on urlah of this type:

 http://site.com/user/1 

In HTACESS prescribed this:

 RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\ /([^\ ]+)\?id RewriteRule ^/?(.*)\?id$ /$1 [L,R=301] 

But it does not give anything :( Help me figure it out, thanks in advance!

UP: Here is this code

 RewriteRule ^([a-zA-Z0-9-_]+)$ user.php?id=$1&%{QUERY_STRING} [NC,L] 

He was able to achieve that url was of this type:

 http://site.com/1 

where "1" is an id, which is successfully transmitted via $ _GET ['id'], but I need to make the URL like this:

 http://site.com/user/1 

That is to add before the "1" another user / Help :)

  • You definitely want to make the user follow the link or enter http://site.com/user?id=1 into the address bar (and see exactly that), and Apache sent the http://site.com/user/1 request to the script (or processed it himself) http://site.com/user/1 ? - tutankhamun
  • When I click on the site.com/user?id=1 link, I want to automatically redirect to this site.com/user/1 URL. At the same time, the "1" should be interpreted by the $ _GET parameter so that I can on the user page. php get user id like this: $ user_id = $ _GET ['id'] - G_Java

3 answers 3

It does not give because the documentation in English is written:

... the pattern is matched against only a partial path ...

If you want a match, use a RewriteCond with the ...% {QUERY_STRING} variables respectively

I translate into human: The expression is compared only with a part of the path without affecting the GET parameters. If you really need to replace it together with the request parameters, you will have to use RewriteCond.

However, in general, your case is not very clear. It seems to me that you have incorrectly formulated a task.

Just in case, I can offer such a solution (well, all of a sudden and really it should be so)

 RewriteCond ${QUERY_STRING} id=([^&]+) RewriteRule ^user/?$ /user/%1 

These strings will look for queries with the GET-parameter id and rewrite them if the query matches the user

     RewriteRule ^(.*)$ /user?id=$1 [L] 

    prescribe user.php to switch to / user / blabla

     RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ RewriteRule ^user$ /user/%1? [L] 

    I hope that helped

    • Thank! But alas ... does not help. - G_Java
    • what is the problem? why does not it work? - L. Vadim
    • I do not know, whatever URL I entered, the redirect does not work. But I still have this in htacess: # browser requests PHP RewriteCond% {THE_REQUEST} ^ [AZ] {3.9} \ / ([^ \] +) \. Php RewriteRule ^ /? (. *) \. Php $ / $ 1 [L, R = 301] # php file: RewriteCond% {REQUEST_FILENAME} \. Php -f RewriteRule ^ /? (. *) $ /$1.php [L] - G_Java

    Redirect (with RewriteCond familiar with hearsay, so that with its syntax could nakosyachit, but for regulars sure ):

     RewriteCond ${QUERY_STRING} ^id=([1-9][0-9]*)(?:&([az\-_]+=[a-z0-9\-_]+))$ RewriteRule ^([az\-_]+)(?:\.php)$ $1/%1?%2 [L,NC,R=301] RewriteCond ${QUERY_STRING} ^id=([1-9][0-9]*)$ RewriteRule ^([az\-_]+)(?:\.php)$ $1/%1 [L,NC,R=301] RewriteRule ^([az\-_]+)(?:\.php)$ $1/ [L,NC,R=301] 

    The second rewrite should work if there are no other parameters except the id , and the third simply replaces the addresses of the form user.php with user/ if they are entered without parameters.

    Reright ( what does ). Added the rewritten=1 parameter so that requests sent by the server do not fall under the redirect, otherwise there will be an endless redirect:

     RewriteRule ^([az\-_]+)(?:(?:\/([1-9][0-9]*))?)$ $1.php?rewritten=1&id=$2&%{QUERY_STRING} [NC,L]