【发布时间】:2017-11-10 08:11:32
【问题描述】:
我的项目在 Blue 主机共享主机以及我的 XAMPP localhost 中运行良好。
为了测试,我们将文件移动到本地 ubuntu 服务器。但是 .htaccess 在这个 ubuntu 服务器上不起作用。
这意味着可以使用 .php 扩展名访问的文件,但在没有 .php 扩展名的情况下访问时会出现 404 错误。
http://172.31.0.55/project/contact.php 有效,但http://172.31.0.55/project/contact 显示 404 错误
这是我使用的 htaccess 代码。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /project/
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project/$1.php -f
RewriteRule ^(.+?)/?$ /project/$1.php [L]
Options All -Indexes
RewriteCond %{HTTP_REFERER} !^http://(www\.)?172.31.0.55/project/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?172.31.0.55/project/.*$ [NC]
ErrorDocument 403 http://172.31.0.55/project/403-error
ErrorDocument 404 http://172.31.0.55/project/404-error
经过一番研究,我在/etc/apache2/apache2.conf中更改了线路
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride none
Require all granted
</Directory>
到
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
但现在尝试访问 http://172.31.0.55/project/ 时显示 500 Internal server error
服务器版本为Apache/2.4.18 (Ubuntu)
它是一个 PHP 网站
我可以在http://172.31.0.55/Apache 默认页面中看到以下代码
The configuration layout for an Apache2 web server installation on Ubuntu systems is as follows:
/etc/apache2/
|-- apache2.conf
| `-- ports.conf
|-- mods-enabled
| |-- *.load
| `-- *.conf
|-- conf-enabled
| `-- *.conf
|-- sites-enabled
| `-- *.conf
我尝试在 htaccess 中将 ip 地址更改为 localhost。但不工作
我检查了 rwx rwx r-x 的文件权限
我参考https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working进行了这些更改
【问题讨论】:
-
检查是否使用 apachectl -l 加载了 rewrite_module
-
Options All -Indexes的位置看起来很奇怪。一般来说,如果您有AllowOverride none,则意味着.htaccess无法正常工作,并且当您启用它时出现500 错误意味着.htaccess文件中可能存在错误。查看 apache 错误日志了解更多信息
标签: php apache .htaccess ubuntu