【问题标题】:Why my site still Internal Server Error on mod rewrite URL?为什么我的网站在 mod rewrite URL 上仍然出现内部服务器错误?
【发布时间】:2017-11-08 04:15:29
【问题描述】:

这是我的 .htaccess 代码

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f

RewriteRule ^test8/([^-]*)$ /test8.php?idval=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

DirectoryIndex index.php
ErrorDocument 404 https://www.example.com/
ErrorDocument 403 https://www.example.com/

当我尝试访问example.com/test8/123456

应该是Internal Server Error

但是当我通过在第一个代码行添加此行来编辑 .htaccess 以用于 mod 重写 URL。

RewriteRule ^test7/([^-]*)$ /test7.php?idval=$1 [L]

喜欢这个

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f

RewriteRule ^test7/([^-]*)$ /test7.php?idval=$1 [L]
RewriteRule ^test8/([^-]*)$ /test8.php?idval=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

DirectoryIndex index.php
ErrorDocument 404 https://www.example.com/
ErrorDocument 403 https://www.example.com/

当我尝试访问example.com/test8/123456 时,它工作得很好。

但是当我尝试访问example.com/test7/123456 时,它将是Internal Server Error

我想知道为什么在 mod rewrite URL 的第一个代码行上仍然 Internal Server Error ?我该怎么办?

【问题讨论】:

  • 我认为RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f 会导致内部错误。如果你评论它,然后尝试它。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

在每个 RewriteCond 之后仅应用 1 个 RewriteRule。要使其适用于 2 个或更多 RewriteRules,请在 RewriteCond 之后添加以下内容:

RewriteRule  .?  -  [S=2]

此行允许为单个 RewriteCond 应用多个 RewriteRules。

所以你的 .htaccess 看起来像:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule  .?  -  [S=2]
RewriteRule ^test7/([^-]*)$ /test7.php?idval=$1 [L]
RewriteRule ^test8/([^-]*)$ /test8.php?idval=$1 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

DirectoryIndex index.php
ErrorDocument 404 https://www.example.com/
ErrorDocument 403 https://www.example.com/

将 [S=2] 更改为需要相应应用的 RewriteRule 的所需数量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多