mod_rewrite导致额外的斜杠

时间:2017-11-15 15:43:54

标签: apache .htaccess mod-rewrite

我试图简单地从URL上的文件名中删除.php。我正在从this StackOverflow回答实现解决方案,所以我的VirtualHost看起来像这样,

# domain: example.com
# public: /var/www/html/example.com/public_html/
<Directory /var/www/html/example.com/public_html>
    Options +FollowSymLinks
     AllowOverride All
    DirectoryIndex index.html index.htm index.php
</Directory>
<VirtualHost *:80>
    ServerName example.com
    Redirect / https://example.com/
</VirtualHost>

<VirtualHost *:443>
        # Admin details
        ServerAdmin vanguard@example.com
        ServerName example.com
        ServerAlias www.example.com

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        #Index File
        DirectoryIndex index.html index.php
        DocumentRoot /var/www/html/example.com/public_html
        #Log Details
        LogLevel warn
        ErrorLog /var/www/html/example.com/log/error.log
        CustomLog /var/www/html/example.com/log/access.log combined
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
</VirtualHost>

转到https://example.com可以解决问题,但如果我尝试转到https://example.com/examplefile,则会将其重写为https://example.com//examplefile/

1 个答案:

答案 0 :(得分:1)

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

在虚拟主机上下文中,此RewriteCond指令永远不会匹配,因为在处理指令时,请求尚未映射到文件系统,因此REQUEST_FILENAME只包含URL路径(与绝对文件系统路径相反),与REQUEST_URI相同。

(这也不会在.htaccess中工作,但原因不同。在.htaccess中,您无法重写为绝对文件系统路径,因此{{1}如果匹配,则RewriteCond可能会触发403 Forbidden错误。)

要在服务器/虚拟主机上下文中修复此问题,您可以修改这些指令以使用基于URL的预测:

RewriteRule

但是,您的原始指令和此更正都不能解释您在示例中看到的双斜线。是错误缓存的响应还是您的Web应用程序?