问题描述
我正在尝试使用重写来添加扩展名,这样 URL 的末尾就不必有扩展名,例如 .html。我之前使用过特定的配置进行重写,我相信它有效,所以我认为它可能是其他原因导致它无法工作,但我对 Apache 不太熟悉。当前发生的只是在尝试访问没有扩展程序的页面时收到 404。
httpd.conf:
ServerRoot "/etc/httpd"
Listen 80
Include conf.d/*.conf
User ...
Group ...
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory "/var/www">
AllowOverride All
Options FollowSymLinks
# Allow open access:
Require all granted
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.PHP -f
RewriteRule (.*) $1.PHP [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
</IfModule>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorDocument 401 /error/
ErrorDocument 403 /error/
ErrorDocument 404 /error/
ErrorDocument 408 /error/
ErrorDocument 500 /error/
ErrorDocument 502 /error/
ErrorDocument 503 /error/
ErrorDocument 504 /error/
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/plain .asc
AddType text/plain .sha512
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
特定主机的配置(请注意,代理用于不同的路径,而不是我试图重写的路径):
<VirtualHost *:443>
DocumentRoot ...
ServerName ...
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLEngine on
SSLCertificateFile ...
SSLCertificateKeyFile ...
SSLCertificateChainFile ...
<Files ~ "\.(cgi|shtml|phtml|PHP3?)$">
SSLOptions +StdEnvVars
</Files>
#ProxyPass ...
#ProxyPassReverse ...
#ProxyPass ...
#ProxyPassReverse ...
browserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
</VirtualHost>
模块配置文件也有这个:
LoadModule rewrite_module modules/mod_rewrite.so
解决方法
问题是 REQUEST_FILENAME 没有提供完整的系统路径。修复的两个选项是将其放在 a 中或使用“%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}”