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

Apache 301通过.htaccess将查询字符串重定向到SEO友好URL,apache,.htaccess,url-rewriting,seo,Apache,.htaccess,Url Rewriting,Seo,我在.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=la

我在.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
color=green
永远不会更改,而
pattern=$1
可以是许多不同的值

上面的代码工作得很好。然而,问题是我现在被两个指向完全相同内容的URL困住了。为了解决这个问题,我想301重定向旧的,丑陋的查询字符串到SEO友好的URL。到目前为止,我所尝试的都不起作用——谷歌今天也不是特别友好


有谁能提供工作代码来放入我的.htaccess文件,将丑陋重定向到新的URL,同时保留重写?谢谢

未测试,但这可能有效


重写规则^directory/script.php?size=large&color=green&pattern=(.*)$/seo/$1/?[R=301,NE,NC,L]

未测试,但这可能有效


重写规则^directory/script.php?size=large&color=green&pattern=(.*)$/seo/$1/?[R=301,NE,NC,L]

这应该可以做到:

重新启动发动机

## 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


希望这能有所帮助。

这应该可以做到:

重新启动发动机

## 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


希望这有帮助。

添加了更多示例URL以进行澄清。添加了更多示例URL以进行澄清。感谢您的回答;恐怕我没法让它工作。没有重定向。谢谢你的回答;恐怕我没法让它工作。没有重定向。谢谢你的回答。我试过你的密码;最初有一个内部服务器错误,这是由代码最后一行的一些额外间距引起的,我删除了它。现在,代码将导致重定向循环。您的代码是打算与我的代码一起运行,还是打算完全替换它?如果我删除我的代码,重新定向是成功的,但它会导致404错误(似乎URL不再被重新编写),我将非常感谢您的评论。嗯,删除了额外的空间,一定是在我粘贴答案时发生的。不管怎么说,它是用来取代你的。不过,我现在确实看到了不一致之处。您的初始规则设置为匹配以
/seo
开头的URL,但是您的示例重定向URL是
/directory/seo
。我的重写设置为与后者匹配。如果你能澄清这一点,我们应该能够让这个工作。感谢你的回复,我们已经设法让这个工作使用我们的代码组合如下:
RewriteCond%{REQUEST_URI}!seo
RewriteCond%{QUERY_STRING}^size=large&color=green&pattern=([a-zA-Z0-9_-]*)$
RewriteRule(.*)/directory\/seo\/%1\/?[L,R=301]
RewriteRule^seo/([^/]*)/$/directory/size=large&color=green&pattern=$1&rewrite[L]
感谢您的回答。我试过你的密码;最初有一个内部服务器错误,这是由代码最后一行的一些额外间距引起的,我删除了它。现在,代码将导致重定向循环。您的代码是打算与我的代码一起运行,还是打算完全替换它?如果我删除我的代码,重新定向是成功的,但它会导致404错误(似乎URL不再被重新编写),我将非常感谢您的评论。嗯,删除了额外的空间,一定是在我粘贴答案时发生的。不管怎么说,它是用来取代你的。不过,我现在确实看到了不一致之处。您的初始规则设置为匹配以
/seo
开头的URL,但是您的示例重定向URL是
/directory/seo
。我的重写设置为与后者匹配。如果你能澄清这一点,我们应该能够让这个工作。感谢你的回复,我们已经设法让这个工作使用我们的代码组合如下:
RewriteCond%{REQUEST_URI}!seo
RewriteCond%{QUERY_STRING}^size=large&color=green&pattern=([a-zA-Z0-9_-]*)$
RewriteRule(.*)/directory\/seo\/%1\/?[L,R=301]
RewriteRule^seo/([^/]*)/$/directory/size=large&color=green&pattern=$1&rewrite[L]