【问题标题】:htaccess redirect from index.php to / only in specific folderhtaccess 从 index.php 重定向到 / 仅在特定文件夹中
【发布时间】:2016-09-27 14:42:33
【问题描述】:

我想在我的 htaccess 文件中创建重定向。从带有 index.php 的 url 到没有 index.php 的页面,但仅限于一个特定的文件夹“目录”。

例如:

/catalog/some_folder/index.php/catalog/some_folder/

/catalog/some_folder/etc/index.php/catalog/some_folder/etc/

/catalog2/some_folder/index.php 这样的网址不必重定向

我尝试添加这些行(本地主机,因为它是 xampp):

  RewriteCond %{REQUEST_URI} /catalog/+[^\.]+index.php$
  RewriteRule ^index\.php$ localhost/$1 [R=301,L]

但这没有用。我怎样才能强制它正常工作?

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:
    RewriteCond %{REQUEST_URI} /index.php
    RewriteRule ^(.*)index\.(php)$ https://%{SERVER_NAME}/$1 [R=301,L]
    

    如果需要删除地址末尾的所有“index.php”

    【讨论】:

      【解决方案2】:
      RewriteEngine On
      RewriteCond %{THE_REQUEST} (?:/[^/]*)*/index\.php[?#\ ]
      RewriteRule .* %{REQUEST_URI}/../ [L,R=301]
      

      对我来说,这是可行的,上面的解决方案是将我发送到上一层的文件夹。

      【讨论】:

        【解决方案3】:

        您可以在 root .htaccess 中使用此规则:

        # remove index.php
        RewriteCond %{THE_REQUEST} /index\.php [NC]
        RewriteRule ^(catalog/.+)index\.php$ /$1 [L,R=302,NC,NE]
        

        【讨论】:

        • 我的配置一定有问题,它把我送上一级(我在 localhost/myproject/index.php 中)和(它把我送到 localhost/)
        • 要使其在/myproject 中工作,只需将最后一条规则中的目标URI 更改为/myproject/$1
        【解决方案4】:

        要删除 index.php:

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php?/$1 [L]
        

        记住,你需要激活 mod_rewrite。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-09
          • 1970-01-01
          • 2013-04-19
          • 2017-09-08
          • 2021-12-11
          • 1970-01-01
          相关资源
          最近更新 更多