duandushang5148
2016-10-24 20:48
浏览 56
已采纳

使用.htaccess中的查询字符串重定向URL [重复]

This question already has an answer here:

I'm trying to migrate an old site to a new one built with concrete5. The old site uses query strings, which I cannot redirect from within concrete5.

  1. There are multiple categories, how can I redirect just the query part?

    • Old URL: example.com/portfolio/category?cat=hifi
    • New URL: example.com/projecten/hifi
  2. Further I have URLs with a different query also needing redirects:

    • Old URL: example.com/portfolio/post.php?s=pagename-xxx-xxx
    • New URL: example.com/projecten/pagename-xxx-xxx

Help very much appreciated!

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 参考:mod_rewrite,URL重写和”漂亮的链接“解释 4 答案

    我正在尝试将旧网站迁移到 用混凝土建造的新建筑物5。 旧站点使用查询字符串,我无法在具体内容中重定向。

    1. 有多个类别,如何仅重定向查询部分?

      • 旧网址: example.com/portfolio/category?cat=hifi
      • 新网址: example.com/projecten/hifi
    2. 我还有其他查询的网址也需要重定向: \ n

      • 旧网址: example.com/portfolio/post.php?s=pagename-xxx-xxx
      • 新网址: example.com/projecten/pagename-xxx-xxx

        非常感谢帮助!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumengmian1180 2016-10-24 22:11
    已采纳

    You'll need to use mod_rewrite and a condition (RewriteCond) that matches against the QUERY_STRING server variable.

    Try the following in your root .htaccess file above any existing mod_rewrite directives.

    RewriteEngine On
    
    # PART 1 : Redirect old category URLs
    RewriteCond %{QUERY_STRING} ^cat=(\w+)
    RewriteRule ^portfolio/category$ /projecten/%1? [R=302,L]
    
    # PART 2 : Redirect other old URLs
    RewriteCond %{QUERY_STRING} ^s=(pagename-\w{3}-\w{3})
    RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
    

    This assumes that the xxx in pagename-xxx-xxx are 3 literal word characters (ie. a-z, A-Z, 0-9 or _).

    UPDATE#1: The trailing ? on the RewriteRule substitution is necessary in order to remove the query string from the target. Or, use the QSD flag on Apache 2.4+.

    Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK. 301 redirects are cached by the browser, which can make testing problematic.

    UPDATE#2: With respect to the updated URL in "PART 2", try the following instead:

    # PART 2 : Redirect other old URLs
    # To essentially remove the date prefix, eg. "YYYY-MM-DD-"
    RewriteCond %{QUERY_STRING} ^s=/d{4}-/d{2}-/d{2}-(.+)
    RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
    

    ^s=[0-9{4}]+-(.+?)/?[0-9{2}]+-(.+?)/?[0-9{2}]+-(.+?)/?(.*)$

    This is a bit of a mash, but also looks overly complex for what you are trying to achieve? For instance, why do you need to match an optional slash (ie. /?)? Your example URL does not contain any slashes?

    A regex pattern such as [0-9{4}]+ isn't doing what you think it's doing. This would match any of the characters 0123456789{} 1 or more times. What you seem to be trying to do is to match exactly 4 digits. eg. [0-9]{4} (which is the same as /d{4}, using a shorthand character class).

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题