通过 .htaccess 将 301 查询字符串重定向到 SEO 友好的 URL
我在 .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=large
和 colour=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以解决问题:
RewriteEngine On
您还可以根据需要匹配大小和颜色,方法是将它们也更改为正则表达式组,并使用相应的
%N
希望这会有所帮助。
This should do the trick:
RewriteEngine On
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.
未经测试,但这可能有效...
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]