【问题标题】:HtAccess rewriting, removing a part of the text between the first two slashes and adding index.php after itHtAccess重写,去掉前两个斜杠之间的部分文本,后面加上index.php
【发布时间】:2025-12-14 08:35:01
【问题描述】:

我得到了以下网址:

127.0.0.1/abc_123456/default/index/index/

应该改写成:

127.0.0.1/123456/index.php/default/index/index/

所以删除 abc_ 并在其后添加 index.php。问题是数字是可变的,但 abc_ 不是。

我有以下规则:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_

RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2

但这导致 url 被重写为:

127.0.0.1/123456/default/index/index.php/index/

好像我快到了,但我想不通。

提前致谢

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    使用这个简单的规则:

    RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
    

    【讨论】:

      【解决方案2】:

      试试

      RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+)  [NC]
      RewriteRule ^ /%1/index.php/%2 [L]
      

      编辑

      但是,您对另一条规则是正确的,那就是给出错误的规则; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 如果页面不包含 /abc_123456/

      为该规则添加一个额外的条件,如下所示

      #if not abc_
      RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_  [NC]
      #if not already index.php
      RewriteCond %{REQUEST_URI} !^/index\.php$ [NC] 
      RewriteRule ^(.*)$ /index.php/$1 [L]
      

      【讨论】:

      • 给我一个内部服务器错误。条件看起来不错,但规则却不行。
      • @user1194648 如果注释掉上面的规则,直接访问127.0.0.1/123456/index.php/default/index/index/会不会报错。您的 .htaccess 中还有其他规则吗?
      • 直接请求 url 给了我预期的页面。但是,您对另一条规则是正确的,那就是给出错误的规则; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 如果页面不包含 /abc_123456/
      • 感谢您的努力。您的解决方案也有效,尽管我更喜欢上面的 [anubhava],因为它更整洁。
      【解决方案3】:

      例如,如果您需要类似的解决方案,当访问此网址时:

      http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
      

      将访问者重定向到

      http://yoursite.com/subpage1/subpage2/
      

      然后查看链接 - http://*.com/a/15680832/2215124

      【讨论】: