【发布时间】:2016-04-11 20:04:31
【问题描述】:
下面的 .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).
【问题讨论】:
-
可能是.htaccess被禁用,检查apache配置
-
抱歉,没有启用但谢谢!
-
您的 .htaccess 中的每个语句都被忽略了,还是只忽略了一些? (例如,您的 404 页面是否有效?)另外,apache 错误日志中是否有错误?
-
.htaccess 似乎正在运行,404 重定向有效,问题似乎在 apache 2.2 和 2.4 之间处理 .php 重写