【发布时间】: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=home 到 http://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