.htaccess 文件中超过 4,000 条重定向 301 行

Over 4,000 Redirect 301 lines in .htaccess file

我的网站即将重新设计,所有 URL 都在变化。这是我到目前为止整理的内容 http://cricketweb.net/new-design%20htaccess

我所做的一些研究表明,.htaccess 文件中的这么多行会大大降低性能。

我问过我的托管服务提供商,他们说:

"Your info is correct, adding this many redirects to your .htaccess will slow down the load time of your site pretty significantly. Adding these to the Apache conf unfortunately isn't a good idea on a cPanel server. cPanel makes edits to the conf file regularly and might not play nice with your edits. I'd recommend reevaluating the rewrite patterns first. For example, if domain1.com has folders a, b, and c and you want to send visitors to domain1.com/a to domain2.com/a, and the same for b and c, this could be accomplished with 1 rewrite rule. Unfortunately, if the URLs differ in inconsistent ways, like if you want to send visitors to domain1.com/a to domain2.com/asdf and visitors to domain1.com/b to domain2.com/zxcv, we may need to look into editing your apache preconf files with special rules for this domain, so please let me know if you will be able to shrink the number rules by consolidating them or not."

问题是我不知道如何合并这些 301。

有人根据 http://cricketweb.net/new-design%20htaccess 有什么建议吗?

作为快速解决方案,我建议在规则的左侧生成所有这些 .php 文件,这只会将用户重定向到右侧的 URL。

据我所知,无法使用正则表达式从旧的 URL 中创建新的 URL。

无法优化类型 A -> B 的关系。

但是如果可以找到重复模式,
A1 -> B, A2 -> B, A9999 -> B
或 A1 -> B1,A2 -> B2,A9999 -> B9999
你真的可以减少行数。

示例:(超过150行同类型)

Redirect 301 \cricketgames\commercial\brianlaracricket05\demo100.php http://www.cricketweb.net/game/brian-lara-international-cricket-2005/
Redirect 301 \cricketgames\commercial\brianlaracricket05\demo101.php http://www.cricketweb.net/game/brian-lara-international-cricket-2005/
Redirect 301 \cricketgames\commercial\brianlaracricket05\demo102.php http://www.cricketweb.net/game/brian-lara-international-cricket-2005/

可以简化为:(1行)

RewriteRule ^cricketgames/commercial/brianlaracricket\d+/demo\d+\.php$ http://www.cricketweb.net/game/brian-lara-international-cricket-2005/ [NC,R=301]

其他例子(超过160行):

Redirect 301 \cricketgames\commercial\internationalcricketcaptain2000\screenshot5.php http://www.cricketweb.net/game/international-cricket-captain-2000/
Redirect 301 \cricketgames\commercial\internationalcricketcaptain2006\screenshot13.php http://www.cricketweb.net/game/international-cricket-captain-2006/
Redirect 301 \cricketgames\commercial\internationalcricketcaptain2009\screenshot14.php http://www.cricketweb.net/game/international-cricket-captain-2009/

可以简化为:(1行)

RewriteRule ^cricketgames/commercial/internationalcricketcaptain(20\d\d)/screenshot\d+\.php$ http://www.cricketweb.net/game/international-cricket-captain-/ [NC,R=301]

而且你的档案里有那么多同类型的病例。

然后您还可以选择将文件分成几个部分(重写),以避免测试从 URL 开头删除的所有行。 例如,这可以将测试数量除以 20 到 30,只需中间重写。