【问题标题】:301 redirect dynamic cgi generated urls using .htaccess301 使用 .htaccess 重定向动态 cgi 生成的 url
【发布时间】:2015-12-04 16:08:58
【问题描述】:

我无法理解我在新工作中继承的这个 .htaccess 文件。我是一名平面设计师/营销人员,学习网络开发。我们的网站有一堆重复错误,我想重命名我们的链接并将它们 301 重定向。

这是 .htaccess 文件的第一部分:

RewriteEngine Off

RewriteCond %{HTTP_HOST} !^blog.asseenontvhot10.com$ [NC]
RewriteCond %{REQUEST_URI} !/sitemap\.xml

AddType text/html .shtml, .html, .htm


AddHandler server-parsed .shtml, .html, .htm
AddHandler application/x-httpd-php5 .html .htm .shtml
#Options FollowSymLinks Includes -Indexes

据我了解,第一部分将我们的博客链接到主站点,我不确定下一行是做什么的。 addtype 行和后面的行允许 .htaccess 读取/写入这些文件类型?

这是我认为相关的部分以及我需要更改的部分:

RewriteEngine on

rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]
RewriteRule default.html /home.html
RewriteRule contact\.html /cgi-bin/commerce.cgi?display=contact
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteRule register\.html /cgi-bin/commerce.cgi?register=action
RewriteRule products\.html /cgi-bin/commerce.cgi?listcategories
RewriteRule parent_category/(.*)/(.*)/ /cgi-bin/commerce.cgi?listcategories=action&parent=$1
RewriteRule category/(.*)/(.*)/(.*)\.html /cgi-bin/commerce.cgi?search=action&category=$1&page=$3
RewriteRule category/(.*)/(.*)/ /cgi-bin/commerce.cgi?search=action&category=$1
RewriteRule category/(.*).html /cgi-bin/commerce.cgi?search=action&page=$1
RewriteRule product/(.*)/(.*)/ /cgi-bin/commerce.cgi?preadd=action&key=$1
RewriteRule basket\.html /cgi-bin/commerce.cgi?display=basket
RewriteRule log-in\.html /cgi-bin/commerce.cgi?login
RewriteRule home\.html /cgi-bin/commerce.cgi?display=home
RewriteRule search\.html /cgi-bin/commerce.cgi?display=search
RewriteRule all-items\.html /cgi-bin/commerce.cgi?search
RewriteRule update_user\.html /cgi-bin/commerce.cgi?displayuser=action
RewriteRule order_history\.html /cgi-bin/commerce.cgi?orderhistory
RewriteRule logout\.html /cgi-bin/commerce.cgi?logout
RewriteRule specials\.html /cgi-bin/commerce.cgi?search=action&keywords=nav_specials

我知道第一部分强制 www。并执行 301 重定向,即使我在阅读过的任何指南中都没有看到这种实现方式。我真正需要更改的第一件事是使主页 301 从以下位置重定向: http://www.asseenontvhot10.com/cgi-bin/commerce.cgi?display=homehttp://www.asseenontvhot10.com/

据我所知,RewriteRule 使这是一个选项,但不会强制它。将 [r=301] 添加到重写工作的末尾吗? 我还读到您需要添加一些代码才能使 .cgi 像 .php 一样工作,但我完全迷路了。


更新: 我尝试将此行添加到底部 -

Redirect 301 /cgi-bin/commerce.cgi?display=home /

我收到了 500 错误。


第二次更新: 我添加了这段代码:

RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about [R=301]

而且它是 404 的。当我用备份替换 .htaccess 文件时,关于页面仍然是 404 ......现在我真的很茫然,不知道如何恢复网站。我什至不知道重定向一旦恢复后将存储在 htaccess 文件之外的位置。


第三次更新:(取得进展) 我添加了这段代码:

RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about\.html? [R=301]

我得到了一个重定向循环。它终于重定向到 about.html,我认为第一条规则重命名为 /cgi-bin/commerce.cgi?display=about,但它一遍又一遍地重定向到它。

【问题讨论】:

  • Add* 与重写无关。那只是告诉apache某些文件扩展名的类型是什么。例如AddType text/html .exe 会让您将 .html 文件命名为 .exe,它们将作为 html 而不是可执行文件提供。
  • [r=301] 只是告诉重写引擎进行客户端重定向,而不是纯粹的服务器内重写
  • 所以我的添加类型规则告诉服务器将 .html 文件作为 .shtml、.html 或 .htm 提供?这是有原因的吗,比如mac兼容性?至于重定向,在规则末尾添加 [r=301] 是否会使浏览器和搜索引擎爬虫将 cgi 链接重定向到干净的链接?
  • 它所做的只是告诉服务器 .htm、.html 和 .shtml 是 html 类型,这通常意味着它们在没有任何服务器端处理的情况下被解雇了。至于重写规则,对爬虫可见的唯一规则是 r=301 。其他一切都只是内部的,例如将“友好的 url”撤消为内部的“脏”。
  • 网站通过 cgi 的事实是否意味着我需要在 .htaccess 文件中添加任何内容?顺便谢谢你的帮助!

标签: apache .htaccess mod-rewrite redirect cgi


【解决方案1】:

Apache 中的 Redirect 指令要求重定向目标是一个完全限定的 URL,带有一个方案和一个 URL。所以这会起作用:

Redirect 301 /cgi-bin/commerce.cgi http://www.yourdomain.com/

但是,我不相信Redirect 可以检查查询字符串,因此为了仅在查询字符串为“display=home”时应用此重定向,您可能必须像这样使用 mod_rewrite:

RewriteCond %{QUERY_STRING} ^display=home$
RewriteRule ^cgi-bin/commerce\.cgi$ / [R=301]

RewriteCond 检查查询字符串是否完全等于“display=home”,RewriteRule 只有在满足此条件时才会触发。在RewriteRule 中,[R=301] 标志指示 mod_rewrite 将访问者的 Web 浏览器重定向到目标路径,在本例中为 /

顺便说一下,查看您的 .htaccess 文件中的现有条件,我看不出您的用户如何在他们的网络浏览器中看到 /cgi-bin/commerce.cgi 路径。所有这些RewriteRule 指令都用于静默重写。这意味着用户将继续在他们的 Web 浏览器中看到“友好”的 URL,但在后台 Apache 将实际找到位于 /cgi-bin/commerce.cgi 路径的文件并返回该文件的内容或结果给用户。因此,您的用户应该已经看到了 URL 的友好版本。如果您尝试做的是静默重写对“/”(站点根目录)的请求,以便 Apache 静默重写路径并实际获取真实文件 /cgi-bin/commerce.cgi?display=home 中的内容,那么你可能想要这个:

RewriteRule ^$ /cgi-bin/commerce.cgi?display=home

这将只匹配对根目录的请求(未指定文件名),并将静默获取并返回通过使用“display=home”参数调用 commerce.cgi 文件产生的内容。

【讨论】:

  • 我添加了这段代码:RewriteRule about\.html /cgi-bin/commerce.cgi?display=aboutRewriteCond %{QUERY_STRING} ^display=about$RewriteRule ^cgi-bin/commerce\.cgi$ /about\.html? [R=301],我得到了一个重定向循环。它最终重定向到 about.html,我认为第一条规则重命名为 /cgi-bin/commerce.cgi?display=about,但它一遍又一遍地重定向到它。
  • mod_rewrite 指令适用于每个请求,包括重写的请求。所以告诉 Apache 将 A 重写为 B,并将 B 重写为 A 将导致无限循环。如果他们请求真实的 URL,我不建议重定向用户。相反,只需将canonical link element 添加到您的 HTML 页面,以便 Google 等搜索引擎知道这只是查找相同资源的另一种方式。
  • 这就是我在这个文件中所做的吗?我真的只是希望 /cgi-bin/commerce.cgi?display=about 重定向到 /about.html 与类别和主页相同。
  • 我认为我不能将规范链接元素添加到 cgi-bin 页面。我很确定它的工作方式是拉出 PDG(我们的商家程序)引用的页面并使用他们的链接。我可以更改 .html 页面中的一些链接,但一些链接是由 PDG 生成并链接到 cgi-bin。
猜你喜欢
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 2021-04-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多