Rewriting URL with selected query string parameters in .htaccess

后端 未结 3 1454
无人及你
无人及你 2020-12-17 23:21

I recently changed my CMS, and want to rewrite some of my URLs to match the new URL/query string parameter format.

The old URL was:

http://www.mysite         


        
3条回答
  •  攒了一身酷
    2020-12-17 23:31

    Here is another version to get specifically what the OP meant. @smhmic's answer is close to this. I just prefer explicit beginning-or-ampersand to find the parameter, rather than the word-boundary approach. (The accepted answer is overly complicated, and has some issues.)

    RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]*)
    RewriteRule ^search\.cgi$ /?s=%1 [NC,R=302]
    

    This is saying:
    (?: ) don't capture this as a % number
    ^|& at beginning of querystring or after an ampersand (this acts like the word boundary, but specifically for urls).
    tag= the param we're looking for.
    ( ) capture this, into %1.
    [^&]* zero or more characters not an ampersand (and stopping at ampersand or running to the end).

提交回复
热议问题