【问题标题】:redirect (mod_rewrite) specific page to new domain将(mod_rewrite)特定页面重定向到新域
【发布时间】:2012-03-12 07:44:02
【问题描述】:

我想将一组页面重定向到新域,但到目前为止我无法实现。

我想要任何以以下开头的请求:

http://www.mydomain.com/wiki/index.php?title=CSV_to_QuickBooks_Converter

被重定向到:

www.StatementConverter.com

例子:

http://www.mydomain.com/wiki/index.php?title=CSV_to_QuickBooks_Converter_-_ABCXYZ

应该被永久重定向到:

http://www.StatementConverter.com

我的 .htaccess 看起来像这样:

# Prevent viewing of the .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Cache files for one month
<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

Options +FollowSymLinks 
RewriteEngine on
RewriteBase /

# trying to redirect these pages to new website
RewriteCond %{QUERY_STRING} ^(.*)CSV_to_QuickBooks_Converter$ [NC]
RewriteRule ^.*$ http://www.statementconverter.com/ [R,L]

到目前为止,显示的是原始页面(未应用我的规则)。

解决方案:

结合我收到的答案,我正在使用它并且它有效:

RewriteCond %{QUERY_STRING} ^title=CSV_Statement_to_QuickBooks_Converter [NC] 
RewriteRule ^.*$ http://statementconverter.com/? [R,L]

我添加了“?”在 RewriteRule 的末尾,以防止将原始 query_string 附加到新 URL。

【问题讨论】:

  • 结合这两个答案,这就是我正在使用的:RewriteCond %{QUERY_STRING} ^title=CSV_Statement_to_QuickBooks_Converter [NC] RewriteRule ^.*$ http://statementconverter.com/? [R,L]

标签: apache mod-rewrite redirect


【解决方案1】:

这可能与 ^ 和 $ 字符有关。试试:

RewriteCond %{QUERY_STRING} \btitle=CSV_to_QuickBooks_Converter [NC]
RewriteRule ^.*$ http://www.statementconverter.com/? [R,L]

在您彻底测试规则并准备好上线之前,请勿发送 301 重定向。 301 重定向被浏览器和搜索引擎缓存。虽然您可以在更改重定向规则后清除浏览器缓存,但您不能总是告诉搜索引擎返回,因为您更改了重定向 URL。

【讨论】:

  • 我已经修改了答案并添加了?。这应该清除查询字符串。
【解决方案2】:

如果您希望重定向以该 URL 开头的任何请求,请不要将正则表达式锚定在 RewriteCond!

你有:

# trying to redirect these pages to new website
RewriteCond %{QUERY_STRING} ^(.*)CSV_to_QuickBooks_Converter$ [NC]
                                                            ^ anchored to exact match!
RewriteRule ^.*$ http://www.statementconverter.com/ [R,L]

确切的 URL ...CSV_to_QuickBooks_Converter 也不会重定向吗?

此外,如果您使用浏览器进行测试,请执行 shift-reload;也许你正在成为cachin的受害者。

顺便说一句,^(.*) 毫无意义!锚定到开头,然后多次咀嚼任何字符,允许其余字符匹配字符串中的任何位置! :) 括号也毫无意义,因为您没有使用捕获的文本或将正则表达式运算符应用于括号组。 (这只是一种习惯,因为经常将(.*) 捕获到$1 中,对吧?哈哈。)

【讨论】:

    【解决方案3】:

    尝试复制粘贴:

    # Prevent viewing of the .htaccess file
    <Files .htaccess>
        Order allow,deny
        Deny from all
    </Files>
    
    # Cache files for one month
    <FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf)$">
        Header set Cache-Control "max-age=2592000"
    </FilesMatch>
    
    Options +FollowSymLinks 
    RewriteEngine on
    RewriteBase /
    
    # trying to redirect these pages to new website
    RewriteCond %{QUERY_STRING} (^|&)title=CSV_to_QuickBooks_Converter [NC]
    RewriteRule (.*) http://www.statementconverter.com/ [R,L]
    

    告诉我它是否有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2010-12-06
      • 2019-12-12
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多