【问题标题】:removing index.php from subfolder using htaccess not working使用 htaccess 从子文件夹中删除 index.php 不起作用
【发布时间】:2015-01-20 10:44:16
【问题描述】:

我正在尝试从我的子文件夹 url 中删除 index.php。所以如果有人访问 www.mysite.com/sub-folder/index.php 我希望 url 重定向到 www.mysite.com/sub-folder/。 我在子文件夹中添加了以下 htaccess 文件,但它不起作用。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sub-folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sub-folder/index.php [L]
</IfModule>

谁能帮我解决这个问题?谢谢

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    你可以在/subfolder/.htaccess中使用这个.htaccess:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /sub-folder/
    
    # remove index.php from URLs
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ $1 [L,R=302,NC,NE]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </IfModule>
    

    【讨论】:

    • 它的工作,但如果文件夹中不存在文件,它不会返回到 404 页面,而是返回索引页面内容我的意思是例如“page.php”文件不在“子文件夹”中,但如果我打电话'www.mysite.com/subfolder/page.php' 这个文件正在从 'subfolder' 获取 index.php 的内容
    • 404 不会被返回,因为您将每个非文件和非目录都发送到 index.php
    • 你好,如何通过htaccess将mysite.com/8to10differentsubfolders/index.php OR mysite.com/8to10differentsubfolders/转为mysite.com/8to10differentsubfolders.php?注意:所有子文件夹中只有index.php 文件。
    • 请发布一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多