【问题标题】:Trying to make an internal redirect from "example.com/about" to "example.com?p=about"试图进行从“example.com/about”到“example.com?p=about”的内部重定向
【发布时间】:2018-03-23 10:39:21
【问题描述】:

我正在尝试从“example.com/about”到“example.com?p=about”进行内部重定向(不更改客户端浏览器中的 url)。

我将以下代码添加到我的 .htaccess 文件中:

RewriteEngine On

RewriteCond %{THE_REQUEST} !\?
RewriteRule ([^\/]*)$ http://%{HTTP_HOST}?p=$1 [L]

但如果我打开页面 (example.com/about),我会收到 内部服务器错误

有谁知道我做错了什么?

感谢您的帮助

编辑:如果我将 R 标志添加到 RewriteRule,一切正常

【问题讨论】:

    标签: .htaccess redirect mod-rewrite internal internal-server-error


    【解决方案1】:

    您遇到错误是因为您的规则只是添加了查询字符串,然后再次捕获之前通过此规则的任何 URI,您的规则应如下所示:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)$ http://%{HTTP_HOST}?p=$1 [L]
    

    为了更具体并能够通过任何其他规则处理真正的错误请求,请执行以下操作:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} !p=(.*)
    RewriteRule ^([^/]*)$ http://%{HTTP_HOST}?p=$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-13
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多