我试图简单地从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/
答案 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应用程序?