我试图重定向多个路径一些动态路径名称,可以是任何东西.
例如:
http://www.aaa.com/ => http://www.bbb.com/nl/
http://www.aaa.com/en/ => http://www.bbb.com/en/
http://www.aaa.com/portfolio/(dynamic-name) => http://www.bbb.com/nl/media
http://www.aaa.com/en/portfolio/(dynamic-name) => http://www.bbb.com/en/media
http://www.aaa.com/media/(dynamic-name) => http://www.bbb.com/nl/media
http://www.aaa.com/en/media/(dynamic-name) => http://www.bbb.com/en/media
http://www.aaa.com/team => http://www.bbb.com/nl/about-us
http://www.aaa.com/en/team= > http://www.bbb.com/en/about-us
这是我现在的代码.
RewriteEngine on RedirectMatch "^/" "http://www.bbb.ai/nl/" RedirectMatch "^/en/" "http://www.bbb.ai/en/" RedirectMatch "^/media/" "http://www.bbb.ai/nl/media/" RedirectMatch "^/media/(.*)" "http://www.bbb.ai/nl/media/" RedirectMatch "^/en/media/" "http://www.bbb.ai/en/media/" RedirectMatch "^/en/media/(.*)" "http://www.bbb.ai/en/media/" RedirectMatch "^/portfolio/" "http://www.bbb.ai/nl/media/" RedirectMatch "^/portfolio/(.*)" "http://www.bbb.ai/nl/media/" RedirectMatch "^/en/portfolio/" "http://www.bbb.ai/en/media/" RedirectMatch "^/en/portfolio/(.*)" "http://www.bbb.ai/en/media/" RedirectMatch "^/team/(.*)" "http://www.bbb.ai/nl/about-us" RedirectMatch "^/en/team/(.*)" "http://www.bbb.ai/en/about-us"
我该如何实现这一目标?
您的问题中的代码表明您可能会混淆两种URL重写技术.RewriteEngine On
提供RewriteRule
功能mod_rewrite
,但是RedirectMatch
是其中的一部分mod_alias
.
正确使用应该为您的方案提供正确的解决方案,但我将在RewriteRule
此处使用,因此请确保mod_rewrite
在您的服务器上启用.我还假设这些规则在你VirtualHost
的.htaccess
文件而不是文件中.
RewriteEngine on # / RewriteRule "^/$" "http://www.bbb.ai/nl/" [R,L] RewriteRule "^/team/?$" "http://www.bbb.ai/nl/about-us" [R,L] RewriteRule "^/media/.+" "http://www.bbb.ai/nl/media/" [R,L] RewriteRule "^/portfolio/.+" "http://www.bbb.ai/nl/media/" [R,L] # /en RewriteRule "^/en/?$" "http://www.bbb.ai/en/" [R,L] RewriteRule "^/en/team/?$" "http://www.bbb.ai/en/about-us" [R,L] RewriteRule "^/en/media/.+" "http://www.bbb.ai/en/media/" [R,L] RewriteRule "^/en/portfolio/.+" "http://www.bbb.ai/en/media/" [R,L]
在根URL处,结束锚$
用于确保只有在不存在其他任何内容时它们才会重定向.对于媒体和投资组合,您在问题中指出,只有在遵循动态内容时才应重定向.
如果你真的想重定向既/portfolio
和/portfolio/something-dynamic
,你可以这样做:
RewriteRule "^/en/portfolio(/.*)?" "http://www.bbb.ai/en/portfolio/" [R,L]
鉴于媒体和投资组合都有相同的目的地,您实际上可以合并如下:
RewriteRule "^/en/(media|portfolio)/.+" "http://www.bbb.ai/en/media/" [R,L]
虽然代码较少,但它的可读性也较低,因此在激活规则时请记住这一点.