【问题标题】:How to remove index.php from URL path except if the path is /index.php/如何从 URL 路径中删除 index.php,除非路径是 /index.php/
【发布时间】:2021-08-17 21:53:43
【问题描述】:

我需要从内页中删除 index.php,以便像 /index.php/path 这样的 URL 路径将只是 /path。但是,如果路径是/index.php/,那么不要删除 index.php 并保留所有内容。我在.htaccess 中尝试以下几行:

RewriteCond %{REQUEST_URI} !^.*/index.php/$ [NC]
RewriteRule /index.php/(.+)$ /$1

但是他们删除 index.php 总是包括路径是/index.php/

【问题讨论】:

    标签: regex .htaccess


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用这两条规则:

    RewriteEngine On
    
    # external redirect to remove /index.php
    RewriteCond %{THE_REQUEST} \s/+index\.php/(\S+) [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the request is not for a valid file
    RewriteCond %{REQUEST_FILENAME} !-f
    # rewrite to /index.php/<uri>
    RewriteRule .+ index.php/$0 [L]
    

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2010-11-29
      • 2016-08-30
      • 2014-07-19
      • 2012-08-09
      • 2018-04-27
      • 2012-07-19
      • 2012-05-15
      相关资源
      最近更新 更多