【问题标题】:Need a Mod rewrite regex solution for .htaccess需要 .htaccess 的 Mod 重写正则表达式解决方案
【发布时间】:2011-04-18 10:42:05
【问题描述】:

从那时起,我将我的网站移至不同的服务器,我的所有网址都开始显示 404 错误。

之前我的所有网址都是这样的 mysite.com/title-of-url

但现在所有此类 url 都出现 404 错误,所以我在站点管理员上禁用了 seo 友好 url 功能,现在我可以访问同一页面 mysite.com/index.php/title-of-url

我厌倦了为 .htaccess 创建一些正则表达式来达到目的 重写规则 ^([^/.]+)/?$ /index.php/$1 [L]

但它在“/”的深度处工作正常,我的意思是它适用于 mysite.com/first 但不适用于 mysite.com/first/second

我正在寻找关于它的 REGEX 帮助,以便 mysite.com/first/second 应该被重写为 mysite.com/index.php/first/second 以及 mysite.com/something 被重写为 mysite.com/index.php/something..

期待您的帮助 谢谢

【问题讨论】:

    标签: regex .htaccess


    【解决方案1】:

    当然,它不适用于更多 /。你的规则说:

    • 从头开始匹配:^
    • (开始为$1分组:()
    • 匹配除斜线以外的所有内容:[^/]+
    • (结束分组:))
    • 匹配可选的前导斜杠/?
    • 匹配到最后$

    如果你真的想匹配所有内容,请使用点而不是字符类,匹配除 / 之外的所有内容:

    RewriteRule ^(.+?)/?$ /index.php/$1 [L]
    

    不要忘记在排除现有文件和目录之前包含RewriteCond,以避免无限递归。如果不这样做,您最终会收到 500 内部服务器错误。完整代码:

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

    【讨论】:

    • 感谢您的输入 Lekensteyn 但我使用了您提到的 RewriteRule ^(.+?)/?$ /index.php/$1 [L] 并最终导致 500 内部服务器错误:(
    • 正则表达式是否可以将 ^mysite.com/depth1/depth2 重写为 ^mysite.com/index.php/depth1/depth2 以及将 ^mysite.com/depth1 重写为 ^mysite。 com/index.php/depth1 在单个重写规则中。我想如果我们将 mysite.com/somethingsomething 重写为 mysite.com/index.php/somethingsomething
    • 您是否在RewriteRule 之前添加了RewriteCond?请参阅我的答案的底部。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多