【问题标题】:Regular expression for DirectoryIndex in htaccesshtaccess 中 DirectoryIndex 的正则表达式
【发布时间】:2017-01-07 01:24:50
【问题描述】:

对不起,如果我重复了一些关于 .htaccess 的讨论。但我没有找到我问题的直接答案。 他们来了: 1. 我正在尝试从主目录和子目录中的 URL 中删除 index.php。 到目前为止,我已经得出以下两条规则(一条用于主目录,另一条用于子目录):

# removes index.php (in public_html)
RewriteRule ^index.php$ / [QSA,R]

# removes index.php (in all directories exept public_html)
RewriteRule ^(.*?)/index\.php /$1 [R=301,R]

有没有办法用一个共同的规则来做到这一点?

  1. 我还有很多子目录索引文件,它们具有以 -index 结尾的不同前缀: bla-index、foo-index、prefix-index 等。我想使用某种正则表达式使它们都作为 DirectoryIndex 工作。 以下规则删除 prefix-index.php

    #removes prefix-index.php (in all directories exept public_html)
    RewriteRule ^(.*)/(.*)index.php /$1 [R=301,R]
    

这正是我想要的,但现在我遇到了 403 错误,因为 htaccess 说:

DirectoryIndex index.php

我为 DirectoryIndex 尝试了不同的正则表达式,但没有成功。请帮忙!

附:我只能使用 htaccess 而不是 httpd.conf

【问题讨论】:

    标签: php apache .htaccess


    【解决方案1】:

    您可以在站点根目录.htaccess 中使用此代码删除index.php

    DirectoryIndex index.php
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s(.*)index\.php[?\s] [NC]
    RewriteRule ^ %1 [L,R=301,NE]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多