【问题标题】:Getting 500 error instead of 404 when add slash添加斜杠时出现 500 错误而不是 404
【发布时间】:2014-04-27 12:32:45
【问题描述】:

我没有找到导致这个错误的规则,我的 .htaccess 是:

RewriteEngine on
Options -Multiviews


RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ru\/index\.php$ "http\:\/\/www\.example\.com\/ru\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^en\/index\.php$ "http\:\/\/www\.example\.com\/en\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^it\/index\.php$ "http\:\/\/www\.example\.com\/it\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^fr\/index\.php$ "http\:\/\/www\.example\.com\/fr\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^de\/index\.php$ "http\:\/\/www\.example\.com\/de\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^uk\/index\.php$ "http\:\/\/www\.example\.com\/uk\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^es\/index\.php$ "http\:\/\/www\.example\.com\/es\/" [R=301,L]

RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule \.php$ - [L,R=404,NC]

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

ErrorDocument 404 /en/404.php
ErrorDocument 403 /error_page.html
ErrorDocument 500 /error_page.html

当我尝试打开不存在的页面时 - 它给了我 404 错误,但如果我将“/”添加到现有页面 - 内部服务器错误 + 错误打开 /error_page.html 文件...为什么?

例如:

http://www.example.com/en/cinque-terre-beaches - real page
http://www.example.com/en/cinque-terre-beachesfdsfs/ - 404
http://www.example.com/en/cinque-terre-beaches/ - 500

谢谢!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite web


    【解决方案1】:

    把你的 php 规则改成这样:

    RewriteCond %{THE_REQUEST} \.php [NC]
    RewriteRule \.php$ - [L,R=404,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    并将其移到其他 R=301 规则之后。

    【讨论】:

    • 通过此更改 incinqueterre.com/de/cinque-terre-beaches.phpSLASH 会重定向到索引(为什么?)并且 incinqueterre.com/de/cinque-terre-beachesSLASH 存在(好的,我将使用规范 url 来解决这个问题)。哦,如果没有双页,我找不到创建漂亮重定向的方法
    • 你为什么要发送\.php URL。以上规则用于处理http://www.incinqueterre.com/de/cinque-terre-beacheshttp://www.incinqueterre.com/de/cinque-terre-beaches/
    • 我希望任何不存在的页面都返回 404 错误。每个页面都有 1 个网址会很好
    • 新规则中缺少某些内容,它给了我一个空白页,其中没有指定输入文件。当我尝试打开存在的 page.php 时出现 *.phpSLASHsomething 和 500 错误
    • 当前规则没有问题,对于.php 的任何直接请求,它将显示 404。您应该将最新的 .htaccess 粘贴到您的问题中
    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2012-05-12
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多