【问题标题】:How to .htaccess redirect file name to be url parameters on new site?如何将 .htaccess 重定向文件名作为新站点上的 url 参数?
【发布时间】:2015-08-25 10:32:30
【问题描述】:

我希望能够将domain.com/cat-1/long-tail-kw.html 等网址重定向到新域和differentdomain.com/goodname.php?id=long-tail-kw 等网址

另外,cat-1 会经常变化,也可能是cat-271 或任何数字。

如何在 .htaccess 文件中执行此操作?

此外,重定向必须与此代码一起使用,该代码仅将来自 bing 的人发送到新网址。

RewriteEngine On
RewriteCond %{HTTP_REFERER} (bing)
RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw

谢谢!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以为此插入新规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_REFERER} (bing)
    RewriteCond %{HTTP_HOST} ^(?:www\.)?sub\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw [L,QSA,R=302]
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?sub\.domain\.com$ [NC]
    RewriteRule ^cat-\d+/(.+?)\.html$ http://differentdomain.com/specificname.php?id=$1 [L,QSA,R=302]
    

    【讨论】:

    • 我试过了,但没有用。我忘了提到它将是子域作为原始网址。像 sub.domain.com/cat-[N]/some-random-url.html 之类的东西,我希望它转到 newdomain.com/specificname.php?id=some-random-url
    • 谢谢,它现在可以工作了.. 虽然我需要删除最后一行并在 Rewriteengine on 之后移动 RewriteCond %{HTTP_REFERER} (bing)
    • 好的,现在在新的浏览器中尝试。基本保持 bing 规则在这条规则之前。
    【解决方案2】:

    RewriteCond 中,您可以使用regular expression。所以我想你可以试试这个:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} domain.com
    RewriteCond %{REQUEST_URI} ^cat-([0-9]+)long-tail-kw\.html$ 
    RewriteRule ^(.*)$ http://differentdomain.com/goodname.php?id=long-tail-kw
    

    【讨论】:

    • 我觉得我表达的不太准确。原始 url 可以是任何东西,但格式类似于 /cat-[N]/some-random-page-title.html,我希望将“some-random-page-title”作为新参数发送网址。
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2020-05-06
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多