下面的 .htaccess 文件适用于各种托管网站,都在 apache 2.2 上运行,但云服务器上的测试环境和新网站在 apache 2.4 上运行。
问题是除非添加包括 .php 在内的完整文件名,否则无法访问 php 文件,例如www.example.com/about.php
而不是 www.example.com/about
非常感谢您对以下内容的任何帮助 - 最好使用适用于 2.2 和 2.4 apache 的 .htaccess。
Options -Indexes +multiviews
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
#ErrorDocument 500 /500.php
# Add www.
RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Remove .php
#RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
#RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
---编辑---
下面是用于 VirtualHost 的 apache 2.4 000-default.conf
php_value auto_prepend_file /vagrant/www/fixdocroot.php
<VirtualHost *:80>
ServerName dev.dev
ServerAlias *.dev
UseCanonicalName Off
VirtualDocumentRoot "/vagrant/www/%-2+/public_html"
<Directory /vagrant/www>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
---进一步编辑--- Apache错误日志如下:
[Mon Jan 11 09:26:35.751982 2016] [core:notice] [pid 1812] AH00094: Command line: '/usr/sbin/apache2'
[Mon Jan 11 09:44:22.816423 2016] [:error] [pid 1892] [client 192.168.33.1:49762] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0, referer: http://speechlink-primary.dev/infant/assessment/start/15
[client 192.168.33.1:49804] AH00687: Negotiation: discovered file(s) matching request: /vagrant/www/domainname/public_html/page-name (None could be negotiated).
最佳答案
从 apache 2.2 升级到 2.4 时可能会出现一些错误
忘记启用模块
有时,您可能会忘记在新服务器上启用使用的模块,这可以通过运行 apache 命令 a2enmod <module>
轻松解决。 ,您正在使用模块 rewrite
在你的 .htaccess 中,这可以通过 a2enmod rewrite
激活.
您使用过的配置参数已重命名
在 apache 2.4 中,配置参数的名称发生了一些变化。
根据apache update notes , 更改了以下内容:
AllowOverride
现在默认为 None
.
参见 @Gal's answer有关此更改的更多信息。
PhP 可能未在新服务器上安装/启用
虽然我怀疑可能是这种情况,但我还是将其包括在内,因为这对其他人来说可能是个问题。
通过制作 phpinfo() 文件检查 php 是否正确安装,该文件包含 <?PHP phpinfo()
并且应该在运行时给出正确的 php 信息。
PHP 可以在 apache 上激活:a2enmod php5
或 a2enmod php7
RewriteRule 日志记录
如果重写规则一直失败,快速测试可能会启用 rewrite rule logs ,这些可以使用默认的 apache“LogLevel”启用,以防止我们的整个 apache 发出调试消息,同时仍然从重写模块获取调试消息,您需要使用它的名称作为后者的前缀。例如:
LogLevel alert rewrite:trace3
这将打开到 trace3 的重写级别调试消息,您可以上升到 lvl trace8
.
rewrite 模块的调试信息可以这样解读:
[rewrite:trace2][rid#7f9c446360a0/initial] init rewrite engine with requested uri /
- 这意味着重写引擎已经为一个 url 启动了
[rewrite:trace3][rid#7f9c446360a0/initial] applying pattern '^.*$' to uri '/'
- 这意味着 RewriteRule
工作了
[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?projects/old-projects/' => not-matched
- 这意味着 RewriteCond
没有匹配。
[rewrite:trace4][rid#7f9c446360a0/initial] RewriteCond: input='/' pattern='^/?' => matched
- 这意味着 RewriteCond
已匹配。
[rewrite:trace2][rid#7f9c446360a0/initial] rewrite '/' -> '/index.html'
- RewriteRule 在内部/外部将您重定向到另一个页面
阅读调试日志文件的最佳方法是使用不换行的文本编辑器,例如 less -R
在 Linux 上或 Windows 上的记事本上。
关于php - .htaccess for apache 2.2 不适用于 apache 2.4 vagrant development box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655032/