2016-09-14 102 views
0

我在我的ubuntu计算机上有一个测试网站,URL是:mysite.test。我有营销网页:http://mysite.test/hello.phphttp://mysite.test/goodbye.php删除.htaccess中的.php扩展名不起作用

我也有通配符子域:http://user1.mysite.test/page1.phphttp://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> 

我缺少什么?谢谢。

+0

会发生什么,如果有人打你的网站与'/富/酒吧/巴兹'?你会改写成'foo/bar/baz.php' e目录存在? –

+0

@ marc-b我在.htaccess文件中添加了''RewriteCond%{REQUEST_FILENAME}!-d'',它仍然不起作用。 –

回答

0

我不知道我可以给你一个有用的解决您的问题,但我有一个类似的一段代码工作:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.|/]+)$ $1.php [NC,L] 

此设置,如果它不是一个文件,如果它不包含.(如图像reference.jpg,它已.php附加到文件名。

这完全适用于我。