【问题标题】:URL query (get parameters like ?id=5) redirecting with mod_rewrite in htaccess在 htaccess 中使用 mod_rewrite 重定向 URL 查询(获取参数,如 ?id=5)
【发布时间】:2012-09-04 13:23:19
【问题描述】:

我现在的htaccess 看起来像

DirectoryIndex welcome.do index.jsp default.php index.wml homepage.php default.htm default.html index.php index.php4 index.php3 index.htm index.html index.shtml default_new_vhost.html

# 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

RedirectPermanent /content.asp?AUID=01 /content/
...

重定向不起作用(我只得到了 Wordpress 的 404 页)。原因是 URL 中的 get 参数。我尝试了here 发布的答案。例如

RewriteCond %{REQUEST_URI} ^/content.asp$
RewriteCond %{QUERY_STRING} ^AUID=01$
RewriteRule ^.*$ http://www.yourdomain.com/content? [L,R=301]

要么我收到 500 错误,要么我来到 Wordpress 的 404 页面。这样做的正确方法是什么?比我在表单中有很多条目

RedirectPermanent /content.asp?pCategory=&pCountry=1&AUID=01 /content/

这是错误的,但我正在寻找一种有效的方法将其放入 htaccess 中。我可以改用这样的东西吗?

RewriteCond %{REQUEST_URI} ^/content.asp$
RewriteCond %{QUERY_STRING} ^pCategory=([0-9]+)&pCountry=([0-6]+)$
RewriteRule ^.*$ http://www.yourdomain.com/content? [L,R=301]

【问题讨论】:

    标签: wordpress apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您只需这样做(从我的网站)

    rewriteCond %{QUERY_STRING} ^id=([0-9]+)$
    rewriterule ^news_details.asp$ news/index.php?old_id=%1
    

    用任何参数替换 ^id 并简单地用户 %1, %2 在重写中访问它们,你的将是

    rewriteCond %{QUERY_STRING} ^pCategory=([0-9]+)&pCountry=([0-6]+)$
    rewriteRule ^content.asp$ /content/?category=%1&country=%2
    

    【讨论】:

    • 感谢您的建议。不知道这是可能的。
    【解决方案2】:

    我最终得到了以下解决方案:

    DirecotryIndex welcome.do index.jsp ...
    
    RewriteCond %{REQUEST_URI} ^/content.asp$
    RewriteCond %{QUERY_STRING} ^AUID=01$ [OR]
    RewriteCond %{QUERY_STRING} ^pCategory=&pCountry=1&AUID=01$ [OR]
    RewriteCond %{QUERY_STRING} ^pCategory=&pCountry=8&AUID=01$
    RewriteRule ^.*$ /content? [L,R=301]
    
    RedirectPermanent /italiano/index.asp /it/
    
    # 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 的编码更改为没有 BOM 的 UTF-8,也可以使用变音符号。您还可以毫无问题地重定向到带有 GET 参数的 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2014-08-01
      • 2021-11-09
      • 2012-11-29
      • 2016-09-20
      相关资源
      最近更新 更多