压缩 .htaccess 文件

发布于 2024-12-29 06:53:30 字数 157 浏览 2 评论 0原文

我有一个 htaccess 文件,其中包含 3,000 多行,这主要归功于我从旧电子商务网站设置的 301 重定向。该文件大小为 323kb,我担心它会成为加载时间和转换的负担。

是否有任何可用的方法可以将文件压缩(缩小?)为更小的大小,或者有人提供更好的想法来处理 301 重定向?

I've got a htaccess file which contains over 3,000 lines mainly thanks to 301 redirects I have setup from my old ecommerce site. The file is 323kb in size and I'm worried it's going to be a burden for load times and therefore conversions.

Is there anything available that can compress (minify?) the file into a smaller size or someone offer a better idea to handle the 301 redirects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2025-01-05 06:53:31

如果重定向是简单的重定向,即 url1url2,没有正则表达式等,并且您可以访问 httpd.conf,那么您可以使用 RewriteMap 用于所有重定向,并且 .htaccess 中可能只有 1 条规则处理这些。

来自 RewriteMap 文档

查找到的键会被httpd缓存起来,直到mapfile的mtime(修改时间)改变,或者httpd服务器重新启动。这可以确保在许多请求调用的地图上获得更好的性能。

If the redirects are simple redirects i.e. url1 to url2, no regex etc, AND you have access to httpd.conf, then you could use a RewriteMap for all the redirects and possibly have just 1 rule in your .htaccess to handle these.

From the RewriteMap documentation

The looked-up keys are cached by httpd until the mtime (modified time) of the mapfile changes, or the httpd server is restarted. This ensures better performance on maps that are called by many requests.

爱她像谁 2025-01-05 06:53:31

您可以指定一些正则表达式来对所有这些重定向进行分组/匹配吗?然后,这提供了两个选项来执行此操作:

  • 第一个是使用 [R=301] 标志使用一组(希望较小的)RewriteRule 语句。
  • 第二个方法是将此重定向移至重定向器脚本中,您可以在其中使用 PHP 逻辑将旧电子商务 URI 解码为其当前格式,然后发出包含 301/302 状态和位置的响应:指向当前 URI 。这还需要您将旧电子商务 URI 全部重写为此重定向器脚本,例如

    RewriteRule ^(product/.*) rewriter.php?uri=$1 [QSA,L]
    

如果没有一些示例,我无法给出更具体的答复。对不起。

Can you specify some regular expressions to group / match all of these redirects? This then offers two options for doing this:

  • The first is to use a (hopefully smaller) set of RewriteRule statements using the [R=301] flag.
  • The second is to move this redirection into a redirector script where you use, say, PHP logic to decode the legacy ecommerce URI into its current format then issue a response with and 301/302 status and Location: pointing to the current URI. This would also need you to do a catch-all rewrite of the legacy ecommerce URIs to this redirector script, e.g.

    RewriteRule ^(product/.*) rewriter.php?uri=$1  [QSA,L]
    

Without some examples, I can't give a more specific reply. Sorry.

2025-01-05 06:53:31

我以前遇到过一些这样的情况,大多数时候你可以用 RewriteRules 替换重定向语句。例如,如果您的网址从:

http://shop.example.com/shop/category/product-id.html

到此:

http://shop.example.com/category/product-id.html

您可以通过如下重写来获取它:

RewriteRule ^/shop/([a-z]+)/([0-9]+)\.html$ /$1/$2.html [L, R=301]

这仍然会导致 301 重定向,因此爬虫仍会知道这是永久移动。

I've had some of these cases before, most of the times you can replace the redirect statments with RewriteRules. For example, if your URL's went from:

http://shop.example.com/shop/category/product-id.html

To this:

http://shop.example.com/category/product-id.html

You can fetch it with a rewrite like this:

RewriteRule ^/shop/([a-z]+)/([0-9]+)\.html$ /$1/$2.html [L, R=301]

This will still result in a 301 redirect, so crawlers will still know it's a permanent move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文