【问题标题】:Remove .php extension in .htaccess not working删除 .htaccess 中的 .php 扩展名不起作用
【发布时间】:2016-09-14 20:47:39
【问题描述】:

我的 ubuntu 电脑上有一个测试网站,网址是:mysite.test。我有营销页面:http://mysite.test/hello.phphttp://mysite.test/goodbye.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 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


【解决方案1】:

我不确定能否为您的问题提供有用的解决方案,但我有一段类似的代码可以工作:

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

如果它不是文件,并且不包含.(例如图像reference.jpg,那么它会在文件名后附加.php

这对我来说非常有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多