繁体   English   中英

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

[英]301 redirect query string to SEO friendly URLs through .htaccess

我在.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中。 为了解决这个问题,我想将旧的,丑陋的查询字符串重定向到SEO友好URL。 到目前为止我所尝试的并不起作用 - 谷歌今天并不是特别友好。

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

这应该做的伎俩:

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

希望这可以帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM