如何在Ubuntu 14.04.2 LTS上使用.htaccess? PHP

时间:2015-07-29 05:37:30

标签: php apache .htaccess ubuntu mod-rewrite

我是新手。我使用的是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]

1 个答案:

答案 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]