通过 .htaccess 将 301 查询字符串重定向到 SEO 友好的 URL

发布于 2024-11-03 13:00:07 字数 824 浏览 1 评论 0原文

我在 .htaccess 文件上编写了一些代码,允许使用 SEO 友好的 URL,而不是丑陋的查询字符串。以下代码将浏览器中的 SEO 友好版本重写为服务器上的查询字符串版本。

RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]

这样丑陋的

http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped

现在变得漂亮了

http://www. mysite.com/directory/seo/striped/

只是稍微解释一下代码; seo是在URL中添加更多关键字,/directory/是.htaccess文件所在目录,参数size=largecolour=green 永远不会改变,而 pattern=$1 可以是许多不同的值。

上面的代码完美运行。然而,问题是我现在遇到了两个指向完全相同内容的 URL。为了解决这个问题,我想将旧的、丑陋的查询字符串 301 重定向到 SEO 友好的 URL。到目前为止我所尝试的方法都不起作用 - 而且谷歌今天也不是特别友好。

任何人都可以提供工作代码放入我的 .htaccess 文件中,将丑陋的重定向到新的 URL,同时保留重写吗?谢谢!

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.

RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]

So that the ugly

http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped

Is now beautiful

http://www.mysite.com/directory/seo/striped/

Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.

The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.

Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-11-10 13:00:07

这应该可以解决问题:

RewriteEngine On

## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]

## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]

您还可以根据需要匹配大小和颜色,方法是将它们也更改为正则表达式组,并使用相应的 %N

希望这会有所帮助。

This should do the trick:

RewriteEngine On

## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]

## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]

You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N

Hope this helps.

白昼 2024-11-10 13:00:07

未经测试,但这可能有效...

RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

Not tested, but this may work...

RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文