在子目录中以htaccess静默添加文件扩展名

时间:2016-05-19 16:10:54

标签: php apache .htaccess mod-rewrite

我最近移动了开发站点,并且在每个开发站点上项目位于子目录中(即:www.site.com/dev_site)。当我切换到新的开发站点(即:www.site2.com/dev_site)时,除非包含.php扩展名,否则站点上的所有链接都不会正常工作。因此,像www.site2.com/account这样的链接会引发404错误:

  

在此服务器上找不到请求的URL / dev_site /帐户

旧开发网站上的前一个htaccess文件工作正常,默默地将.php添加到每个uri的末尾,而不是在url中显示它。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
    RewriteRule ^(.*)$ $1.php [L,QSA]
</IfModule>

我知道mod_rewrite已启用,因为我可以在文件中放入垃圾并抛出500.

我也尝试过:

<IfModule mod_rewrite.c>
    Options All -Indexes -MultiViews
    RewriteEngine on
    RewriteBase /dev_site/

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{DOCUMENT_ROOT}/dev_site/$1\.php -f [NC] 
    RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

所有这一切都是500错误。删除Options All -Indexes -MultiViews只会让我回到之前提到的404未找到的错误。

1 个答案:

答案 0 :(得分:0)

我能够从我在这里找到的答案中解决这个问题:https://stackoverflow.com/a/30487642/2122300

我把它修改为:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews

    RewriteEngine On
    RewriteBase /dev_site/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /dev_site/$1.php [L]
</IfModule>