【问题标题】:Remove query string from URL using MOD_REWRITE使用 MOD_REWRITE 从 URL 中删除查询字符串
【发布时间】:2016-12-12 17:27:20
【问题描述】:

好的,我在使用 mod_rewrite 编写干净的 URL 时遇到了很多问题。 假设我有一个类似

的链接

http://www.example.com/search.php?q=variable 我想把它转换成一个干净的 URL,比如

http://www.example.com/variable

到目前为止,我使用以下重写规则完成了这项工作

RewriteRule ^/?([a-zA-Z_]+)$ search.php?q=$1 [L].

现在,当我直接在浏览器中输入这个干净的 URL 时,它工作正常,但是当我打开我的网站并通过我网站的搜索框查询任何内容时,它仍然显示

http://www.example.com/search.php?q=variable

我只想解决这个问题,因为当我输入干净的 URL 时它工作正常,但是在从搜索框搜索而不是直接在浏览器中写入 URL 时,它仍然显示带有查询字符串的 URL。 非常感谢任何帮助将得到appriciated

【问题讨论】:

    标签: php apache .htaccess mod-rewrite server


    【解决方案1】:

    将您的 HTML 代码更改为 禁用表单提交 并始终使用此 HTML sn-p 使用漂亮的 URL:

    <html>
    <head>
    <script>
    function prettySubmit(form, evt) {
       evt.preventDefault();
       window.location = '/' + form.q.value.replace(/ /g, '+');
       return false;
    }
    
    </script>
    </head>
    <body>
    <form method="get" action="search" onsubmit='return prettySubmit(this, event);'>
       <input type="text" name="q" class="txtfield" 
           value="<?php if (isset($_GET['q'])) echo $_GET['q']; ?>" 
            placeholder="Search Keyword" />
       <input type="submit" name="submit" class="btn" value="Search" />
    </form>
    </body>
    </html>
    

    然后在站点根目录 .htaccess 中设置此规则:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([a-zA-Z_]+)$ search.php?q=$1 [L,QSA]
    

    【讨论】:

    • 非常感谢先生您救了我的一天
    【解决方案2】:

    您可以在以下网站查看所有可用说明: Complete Tutorial about how to clean url with php and .htacess

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2012-05-11
      • 2011-02-02
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多