mod_rewrite: Pass the path & query string URL as a parameter

前端 未结 3 2139
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 13:13

I\'m using mod_rewrite to rewrite pretty URLs to a form supported by a Spring 2.5 application.

e.g. /category/cat1?q=x   =>  /controller?category=cat1&         


        
相关标签:
3条回答
  • 2020-12-18 13:27

    The query can ony be tested with RewriteCond since RewriteRule does only test the URL path. See Jonathan Feinberg’s example how to do that.

    But you could also just set the QSA flag and the old query gets automatically appended to the new one:

    RewriteRule ^/category/([^/]+)$ /controller?category=$1 [QSA]
    

    Edit    In response to your comment to this question: If you want to get the initial requested URI path and query, you need to extract it from the request line (THE_REQUEST variable):

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
    RewriteRule ^/category/([^/]+)$ /controller?category=$1&_originalUrl=%1 [QSA]
    

    But in most languages there is an environment variable with the very same information.

    0 讨论(0)
  • 2020-12-18 13:28

    You have to capture the query string in an initial, separate step.

    RewriteCond %{query_string} ^q=([^&]+)
    RewriteRule ^/category/(cat1)$ /controller?category=$1&q=%1
    

    etc.

    0 讨论(0)
  • 2020-12-18 13:33

    Is there a host header you can use? Sorry for being so vague, but last time I had to do this was using PHP (urgh, dont ask), and I think thats how we did it - eg REQUEST_URI (http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html)

    You also may be able to SET a host header in the rewrite (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)

    eg:

    ReWriteRule /category/(cat1)?q=(x) /controller?category=$1&q=$2 [E=FOO:....]
    

    (and no, I'm so totally NOT a mod-rewrite ninja)

    0 讨论(0)
提交回复
热议问题