301重定向查询字符串通过搜索引擎优化的.htaccess友好的URL [英] 301 redirect query string to SEO friendly URLs through .htaccess

查看:263
本文介绍了301重定向查询字符串通过搜索引擎优化的.htaccess友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在我的.htaccess文件中的一些code允许使用的搜索引擎友好的URL,而不是丑陋的查询字符串。下面code重写搜索引擎优化友好版本浏览器向服务器上的查询字符串版本。

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]

这样丑陋

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

现美丽

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

只是为了解释℃的位$ C $; SEO 有没有增加更多的关键词到URL, /目录/ 是目录中的.htaccess文件是位置,参数尺寸=大颜色=绿色永远不会改变,而模式= $ 1 可以有很多不同的值。

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.

以上code完美的作品。然而,问题是我现在坚持与指向的内容完全相同两个URL。为了解决这个问题,我想301重定向旧的,丑陋的查询字符串的搜索引擎友好的URL。我已经试过到目前为止不起作用 - 和谷歌今天不是特别友好

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.

任何人都可以提供工作code把我的重定向丑陋到新的URL .htaccess文件,同时保留重写?谢谢!

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

推荐答案

这应该做的伎俩:

RewriteEngine叙述在

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

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

希望这有助于。

这篇关于301重定向查询字符串通过搜索引擎优化的.htaccess友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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