【问题标题】:301 redirect with ? and & .asp301重定向与?和 & .asp
【发布时间】:2013-03-01 10:21:49
【问题描述】:

我知道有几篇关于此的文章,但在 .htaccess 文件中的重定向方面我是新手,所以请原谅我的无知!我已经尝试了一些东西,但我无法让它工作。

我有旧网址 http://domain.com/ourlocations.asp?cat=41&id=41 我试图在 .htaccess 中重定向到 http://domain.com/locations 。第一个/旧网址通过二维码包含在打印工作中,所以我有点卡住了。我试过使用简单的 Redirect 301 但没有运气。我看过一些关于使用 RewriteRule 的帖子,但我在 .htaccess (WordPress) 中有现有代码,并且害怕更改以防网站崩溃。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

有人有什么想法吗?感谢您的宝贵时间。

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite redirect


    【解决方案1】:

    这个 RewriteRule 应该将http://domain.com/ourlocations.asp?cat=41&id=41 重定向到http://domain.com/locations

    • RewriteCond 指令检查查询字符串参数。
    • 如果您在 /ourlocations.asp 中,则 RewriteRule 的第一部分匹配
    • RewriteRule 替换部分中的尾随 ? 会删除所有查询参数(如果您希望传递参数,请将其删除)
    • [R=301] 告诉 Apache 进行 301 重定向

    -

    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{QUERY_STRING} cat=41
    RewriteCond %{QUERY_STRING} id=41
    RewriteRule ^ourlocations\.asp http://domain.com/locations? [R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    </IfModule>
    

    【讨论】:

    • 感谢@kjetilh 的回复。不幸的是,我试了一下,它仍然无法正常工作。我尝试了几个不同的选项来添加它,在它外面的 中,添加 RewriteEngine On,但没有一个起作用。这会是服务器设置问题吗?
    • @mickmaster 对不起,brainlag,我看到你在使用 WordPress。在 WordPress 中包含自定义 .htaccess 指令有点棘手,因为它会自动在 .htaccess 文件中生成(并覆盖)指令。我稍后会回来,否则你可以搜索“WordPress custom .htaccess Rewrite rules”之类的东西,看看你找到了什么
    • 感谢您的宝贵时间。我会根据你的建议进行搜索,看看会发生什么。再次感谢。
    • 这是最好的答案。您应该能够为每个可能的查询参数重定向到特定的 URL。
    【解决方案2】:

    感谢大家的帮助,最终我们得到了解决方案:

    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^ourlocations\.asp http://domain.com/locations? [L,R=301]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    </IfModule>
    

    【讨论】:

      【解决方案3】:

      另一个可行的解决方案:

      # END WordPress之后添加:

      RedirectMatch 301 /index.asp(.*) http://www.domain.com/$1
      

      例子:

      www.domain.com/index.asp/string=test&url=22
      

      将重定向到:

      www.domain.com/string=test
      

      现在需要像这样在它后面写 301:

      RewriteCond %{QUERY_STRING} ^string=test&url=22$
      RewriteRule ^$ http://www.domain.com/requiredPage/? [R=301,L]
      

      现在网址:www.domain.com/index.asp/string=test&amp;url=22 将 301 转换为 www.domain.com/requiredPage/

      【讨论】:

        猜你喜欢
        • 2013-01-07
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2014-08-29
        • 2011-02-01
        • 1970-01-01
        相关资源
        最近更新 更多