带有参数的 URL 的 htaccess 301 重定向 [英] htaccess 301 redirect for URL with parameter

查看:32
本文介绍了带有参数的 URL 的 htaccess 301 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 htaccess 文件中有以下内容,它采用如下 URL:www.example.com/Page.php?id=About 并将其转换为 www.example.com/about.唯一的问题是,当我在 htaccess 中设置重定向 301s 时,旧的 URL 没有重定向到新的 URL(我理解这是因为你不应该在基本的重定向 301s 中传递参数 - 我的基本重定向示例下面试过).

I have the following in my htaccess file, which takes a URL like: www.example.com/Page.php?id=About and turns it into www.example.com/about. The only issue is that the old URLs are not redirecting to the new URLs when I set up Redirect 301s in the htaccess (which I understand is because you're not supposed to pass parameters in basic Redirect 301s - example of basic Redirect that I've tried below).

Redirect 301 /Page.php?id=About http://www.example.com/about

问题在于,新 URL 不一定只是删除当前 id 并将其替换为小写版本.例如,我还想:

The catch is that the new URLs don't necessarily just strip out the current id and replace it with a lowercase version. For example, I would also want to:

Redirect 301 /Page.php?id=Example http://www.example.com/example-page

我想保留重写 URL 的当前功能,但也希望对旧 URL 的任何访问都重定向到新 URL(即更新 Google 索引).访问 www.example.com/Page.php?id=About 目前仍然有效,但显示一个没有内容的页面.

I want to keep the current functionality of rewriting the URLs, but also want any visits to the old URLs to redirect to the new ones (i.e. to update Google index). Visiting www.example.com/Page.php?id=About at the moment still works but shows a page with no content.

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9\-]+)/?$ Page.php?id=$1
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ Page.php?id=$1&tab=$2
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ Page.php?id=$1&tab=$2&tabid=$3

推荐答案

如果您将此规则添加到您的 .htaccess,它应该完成重定向的工作.它将添加 ?id=About 到/about 的重定向.如果您在 2.4.0 或更高版本上运行,您可以添加 QSD 标志以丢弃重定向中的查询字符串.

If you add this rule to your .htaccess it should do the work of the Redirect. It will add the ?id=About to the redirect to /about. If you are running on version 2.4.0 or later, you can add the QSD flag to discard of the query string in the redirect.

RewriteCond %{QUERY_STRING} ^id=About$
RewriteRule ^page.php /about [NC,R,L]

这篇关于带有参数的 URL 的 htaccess 301 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆