【问题标题】:How to get these mod-rewrites to work?如何让这些 mod 重写工作?
【发布时间】:2011-02-26 06:20:38
【问题描述】:

我在进行 301 重定向时遇到了 2 个问题。我在该域上拥有的旧 CMS 没有被使用,我正试图弄清楚如果有匹配项如何正确重定向。

1) 从 url 中删除查询字符串并进行重定向。 以下是尝试访问的 urls 搜索引擎应该重定向到主页 index.php

示例:http://www.mysite.com/?dispatch=products.search&features_hash=P2

目标:http://www.mysite.com/

例如:http://www.mysite.com/index.php?dispatch=products.search&features_hash=V9

目标:http://www.mysite.com/

示例:http://www.mysite.com/index.php?dispatch=news.list

目标:http://www.mysite.com/

这是我目前尝试的代码,但不起作用:

RewriteCond %{QUERY_STRING} ^dispatch$  
RewriteRule ^(.*)$ http://www.mysite.com/$1? [R=301,L] 

2) 我遇到的另一个问题是由于某种原因,我的网站通过 url= 参数在搜索引擎中存在重复项。例如,

示例:http://www.mysite.com/?url=http://www.mysite.com

目标:http://www.mysite.com/

基本上它们是相同的页面,但是当您考虑到我有类别页面和其他单个页面都通过 url= 访问时,这是不对的。基本上我想要做的是将 301 返回到主 mysite.com 为此,我尝试了以下代码,但也不起作用:

RewriteCond %{QUERY_STRING} ^url$  
RewriteRule ^(.*)index.php$ http://www.mysite.com/$1? [R=301,L] 

*****-----已编辑:------******* 完成 htaccess 文件:

Options -Indexes
Options +SymLinksIfOwnerMatch
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTP_HOST} ^mysite.com
  RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
  RewriteRule ^([a-z0-9/-]+)-p-([0-9]+).html$ my-pages.php [NC,L,QSA]
</IfModule>

RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*iframe.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteCond %{QUERY_STRING} (;|'|"|%22).*(union|select|insert|drop|update|md5|benchmark|or|and|if).* [NC]
RewriteCond %{QUERY_STRING} ^(.*)/self/(.*)$ [NC]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteRule ^(.*)$ error_page.php [F,L]
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]

ErrorDocument 400 /error_page.php?error_id=400
ErrorDocument 401 /error_page.php?error_id=401
ErrorDocument 403 /error_page.php?error_id=403
ErrorDocument 404 /error_page.php?error_id=404
ErrorDocument 405 /error_page.php?error_id=405
ErrorDocument 408 /error_page.php?error_id=408
ErrorDocument 415 /error_page.php?error_id=415
ErrorDocument 500 /error_page.php?error_id=500
ErrorDocument 501 /error_page.php?error_id=501
ErrorDocument 502 /error_page.php?error_id=502
ErrorDocument 503 /error_page.php?error_id=503
ErrorDocument 505 /error_page.php?error_id=505
ErrorDocument 504 /error_page.php?error_id=504

<FilesMatch "\.(inc|tpl|h|ihtml|sql|ini|conf|class|bin|spd|theme|module|exe)$">
    deny from all
</FilesMatch>

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>

SetEnvIfNoCase User-Agent "^libwww-perl*" block_bad_bots
Deny from env=block_bad_bots
RewriteCond %{HTTP_USER_AGENT} libwww-perl [OR]
RewriteCond %{QUERY_STRING} tool25 [OR]
RewriteCond %{QUERY_STRING} cmd.txt [OR]
RewriteCond %{QUERY_STRING} cmd.gif [OR]
RewriteCond %{QUERY_STRING} r57shell [OR]
RewriteCond %{QUERY_STRING} c99 [OR]

<Files 403.shtml>
order allow,deny
allow from all
</Files>

【问题讨论】:

  • 给出一些示例 URL 和目标 URL。
  • @Zimbabao:完成。根据您的要求添加它

标签: .htaccess mod-rewrite


【解决方案1】:

试试这个

RewriteCond %{QUERY_STRING} ^dispatch  
RewriteRule ^(.*)$ / [R=301,L] 


RewriteCond %{QUERY_STRING} ^index.php\?dispatch  
RewriteRule ^(.*)$ / [R=301,L] 

RewriteCond %{QUERY_STRING} ^url=http://www.mysite.com
RewriteRule ^(.*)$ / [R=301,L] 

【讨论】:

  • @Zimbabo:第一个创建循环并引发错误。 2号不行。我完全按照您发布的内容复制粘贴。请指教。谢谢
  • 这些是你仅有的重写?或者你添加了更多?,如果你有更多,粘贴它们
  • @Zimbabao:完成。我已经发布了整个 htaccess 供您参考。再次感谢
  • 能否请您启用重写日志,RewriteLog /path/to/logdir/rewritelog RewriteLogLevel 9 看看发生了什么。我不知道为什么会这样。
  • @Zimbabao:我无法让 RewriteLog 工作。我不断收到 500 内部服务器错误。
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多