我是新手。我使用的是Ubuntu 14.04.2,并且我在目录.htaccess
中创建了一个/var/www/example.com/.htaccess
文件。但是我的脚本hello.php
无效,而是返回error 500 internal server error
。
第一步:
1. enabling mod-rewrite using
sudo a2enmod rewrite
2. change config
sudo nano /etc/apache2/sites-available/000-default.conf
<Directory "/var/www">
AllowOverride All
</Directory>
3. restart apache2
sudo service apache2 restart
4. create .htaccess in sub folder
sudo nano /var/www/example.com/.htaccess
我希望.htaccess隐藏.php,例如"www.example.com/aaa.php"
到"www.example.com/aaa/"
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
答案 0 :(得分:0)
您可能想尝试一些事情。首先,您不在重定向中包含尾部斜杠。其次,针对-f
变量的%{REQUEST_FILENAME}
检查无法正常工作,因为它模糊并会处理路径信息。所以试试:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]