【发布时间】:2016-09-14 20:47:39
【问题描述】:
我的 ubuntu 电脑上有一个测试网站,网址是:mysite.test。我有营销页面:http://mysite.test/hello.php、http://mysite.test/goodbye.php 等。
我还有通配符子域:http://user1.mysite.test/page1.php、http://user2.mysite.test/page.1php 等。
我希望无需添加“.php”扩展名(营销网站和子域)即可访问所有页面。我修改了我的 .htaccess 文件,但它不起作用,我不知道为什么。 mod_rewrite 已启用并正常工作(下面的“删除 www”规则有效)。
#/var/www/mysite.test/public_html/.htaccess
RewriteEngine On
#Remove www - this works
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC]
#Not need .php extension - this does not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
删除第一条规则(“删除 www”)不会使其开始工作。
当我转到 http://mysite.test/hello 时,我收到 404 Not Found 错误。转到http://mysite.test/hello.php 加载就好了。
当我转到 http://user1.mysite.test/page1 时,我收到 404 Not Found 错误。转到http://user1.mysite.test/page1.php 加载就好了。
我的虚拟主机文件如下所示:
#/etc/apache2/sites-available/mysite.test.conf
<VirtualHost *:80>
ServerName www.mysite.test
ServerAlias mysite.test
ServerAdmin fake@gmail.com
DocumentRoot /var/www/mysite.test/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/mysite.test>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName wildcard.mysite.test
ServerAlias *.mysite.test
DocumentRoot /var/www/mysite.test/public_html/app
<Directory /var/www/mysite.test/public_html/app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
我错过了什么?谢谢。
【问题讨论】:
-
如果有人使用
/foo/bar/baz访问您的网站会怎样?您将重写为foo/bar/baz.php。这些目录存在吗? -
@marc-b 我已将
RewriteCond %{REQUEST_FILENAME} !-d添加到 .htaccess 文件中,但它仍然不起作用。
标签: php apache .htaccess ubuntu