我在我的ubuntu计算机上有一个测试网站,URL是:mysite.test。我有营销网页:http://mysite.test/hello.php,http://mysite.test/goodbye.php等删除.htaccess中的.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 [email protected]
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.php' e目录存在? –
@ marc-b我在.htaccess文件中添加了''RewriteCond%{REQUEST_FILENAME}!-d'',它仍然不起作用。 –