.htaccess 301 使用正则表达式重定向

mac*_*cek 9 regex httpd redirection htaccess

如果我有:

redirect 301 /users/foo http://www.example.com/profiles/foo
redirect 301 /users/bar http://www.example.com/profiles/bar
Run Code Online (Sandbox Code Playgroud)

我可以做类似的事情吗?

redirect 301 ^\/users/(.+)$ http://www.example.com/profiles/$1
Run Code Online (Sandbox Code Playgroud)

编辑

找到了解决办法:

RedirectMatch users/(.+) http://www.exapmles.com/profiles/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)

这实际上是重定向而不是重写。


编辑 2

请参阅@Darth Android 的解决方案,RewriteEngine该解决方案也同样适用:)

Dar*_*oid 13

如果您有 apache,请尝试使用重写规则:
RewriteEngine on
RewriteRule ^/users/(.*)$ http://www.example.com/profiles/$1 [R=301,L]

请注意,您需要在 apache 配置中安装并启用 ModRewrite。如果您需要 IIS 方法,请从此处提取。