如何使这些mod-rewrites工作?

时间:2011-02-26 06:20:38

标签: .htaccess mod-rewrite

我做301重定向有2倍的问题。我在域上使用的旧CMS没有被使用,我试图找出如果匹配的话如何正确重定向。

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

  

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

     

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

OR

  

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

     

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

OR

  

示例: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] 

** * ** ---- --EDITED:------的 ** * **** 完整的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>

1 个答案:

答案 0 :(得分:2)

试试这个

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]