【问题标题】:Apache - Rewrite URL [closed]Apache - 重写 URL [关闭]
【发布时间】:2013-12-01 20:10:49
【问题描述】:

我有一个简单的html表单

<form class="form-search" method="get" action="search.php">
  <input name="what" type="text" class="input-xlarge search-query" placeholder="what">
  <input name="where" type="text" class="input-xlarge search-query" placeholder="where">
  <button type="submit" class="btn btn-primary search">search</button>
</form>

当我点击按钮时,URL 就像http://example.com/search.php?what=foo&where=bar,到目前为止一切正常。

我想要相同的功能,但我想将 URL 重写为 http://example.com/foo/bar/

有没有可能用apache rewrite来实现?

【问题讨论】:

    标签: html regex apache get rewrite


    【解决方案1】:

    您需要匹配请求以使浏览器加载/foo/bar/ URL,然后在内部将其重写返回到查询字符串:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} GET\ /+search\.php\?what=([^&]+)&where=([^&\ ]+)
    RewriteRule ^ /%1/%2/? [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([^/]+)/([^/]+)/?$ /search.php?what=$1&where=$2 [L,QSA]
    

    【讨论】:

      【解决方案2】:

      我认为这应该可行

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^/([^/]+)/([^/]+) /search.php?what=$1&where=$2 [L]
      

      这个网址http://example.com/foo/bar/ 如果没有名为 foo 的文件夹和与文件名匹配的文件或目录,它将重定向显示请求 http://example.com/search.php?what=foo&where=bar 结果。

      【讨论】:

        【解决方案3】:

        http://corz.org/serv/tricks/htaccess2.php?page=1 对您想要做什么有很好的说明。

        Options +FollowSymLinks
        RewriteEngine on
        RewriteRule ^/([^/]+)/([^/])+ /search.php?what=$1&where=$2 [NC]
        

        应该可以解决问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-05
          • 2012-10-02
          • 1970-01-01
          • 2021-02-25
          • 2010-10-30
          • 2023-03-10
          • 2021-03-22
          • 1970-01-01
          相关资源
          最近更新 更多