【问题标题】:htaccess URL rewrite not working for all caseshtaccess URL重写不适用于所有情况
【发布时间】:2014-08-29 07:14:26
【问题描述】:

我想把这个网址 www.example.com/index.php?page=search&q=string1&type=string2 改写成这个 www.example.com/search/?q=string1&type=string2 或 www.example.com/search?q=string1&type=string2。

我使用了代码但不起作用:

 RewriteEngine on
 RewriteRule ^search?([^/]*)$ index.php?page=search&$1 [L,NC]

谁能给出解决方案?

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:

    您可以在/myprojects/www.mysite.com/.htaccess中使用此规则:

    RewriteEngine on
    RewriteBase /myprojects/www.mysite.com/
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /index\.php\?page=(search)\s [NC]
    RewriteRule ^ %1/? [R=302,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^search/?$ index.php?page=search [QSA,L,NC]
    

    QSA(查询字符串附加)标志在添加新参数时保留现有查询参数。

    【讨论】:

    • 我明白了。非常感谢。
    • 您的回答有一个小的更正。应该是 RewriteRule ^search/?$ index.php?page=search [QSA,NC,L]
    • 这是你的选择,我不会要求你改变它,但你接受的答案是不必要的,而且它不起作用。如果您将 URL 输入为/index.php?page=search,则该答案不会改变它/search(第一个 302 规则在那里不起作用)
    • 仅供参考,我已经向您展示了重定向/index.php?page=search URI 的正确方法。
    • 现在您正在揭示最重要的信息。即您在index.php 之前有/myprojects/www.mysite.com/,您没有在任何地方说明。让我用这个来更新答案。
    【解决方案2】:

    在 ^search?([^/]*)$ 中转义 /。所以你的代码应该是:

        RewriteEngine on
        RewriteRule ^search?([^\/]*)$ index.php?page=search&$1 [L,NC]
    

    我使用正则表达式测试器,有时它们会有所帮助 :) 这是一个链接http://regex101.com/

    【讨论】:

      【解决方案3】:

      我想你的意思是redirect

      来自

      www.example.com/index.php?page=search&q=string1&type=string2

      www.example.com/search?q=string1&type=string2

      这适用于这种情况

      RewriteEngine on
      
      # Stop any redirect loop
      RewriteCond %{ENV:REDIRECT_STATUS} 200
      RewriteRule ^ - [L]
      
      # if it does not starts with /search
      RewriteCond %{REQUEST_URI} !^/search
      
      # if the query string start with
      # page=search, then get the rest
      # of the query string
      RewriteCond %{QUERY_STRING} ^page=search&(.*)
      
      # redirect from:
      # ?page=search&q=string1&type=string2
      #
      # to
      # search?q=string1&type=string2
      RewriteRule ^(.*)$ /search?%1 [R=302,L] # change to 301
      
      # if the uri starts with search then
      # process the url and query string previously redirected
      # the flag: QSA 
      # appends the query string
      RewriteRule ^search/?$ index.php?page=search [QSA,NC,L]
      

      并将R=302 更改为R=301

      已编辑以匹配新要求

      RewriteEngine on
      RewriteBase /your/folder/here
      
      RewriteCond %{ENV:REDIRECT_STATUS} 200
      RewriteRule ^ - [L]
      
      RewriteCond %{REQUEST_URI} !^/search
      RewriteCond %{QUERY_STRING} ^page=search[&(.*)]?
      RewriteRule ^(.*)$ /search?%1 [R=302,L]
      
      RewriteRule ^search/?$ index.php?page=search [QSA,NC,L]
      

      【讨论】:

      • 我明白了。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多