Apache RedirectMatch 指令的正则表达式
如果 URL 不以仪表板结尾,我尝试将所有用户请求重定向到不同的 URL。下面是 RedirectMatch 指令,我使用不同的 URL 模式在线正则表达式验证器站点测试了正则表达式,所有站点都显示正则表达式是正确的。
但由于某种原因,重定向没有发生。
RedirectMatch 301 ^/^((?!dashboard).)*$ https://abc.xyz.com/$1
请让我知道我是否在正则表达式中遗漏了一些内容。
阿帕奇配置文件
<VirtualHost *:443>
ServerName staging.abc.com
## Logging
LogLevel info
CustomLog "/var/log/apache2/abc_https_access.log" combined
ErrorLog "/var/log/apache2/abc_https_error.log"
ServerSignature Off
SSLEngine on
SSLProtocol -All +TLSv1.2
SSLCertificateFile /root/san/staging.abc.cer
SSLCertificateKeyFile /root/san/staging.abc.key
SSLCACertificateFile /root/san/abc.chain.cer
SSLVerifyClient require
RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1
## Proxy rules
ProxyRequests Off
ProxyPreserveHost On
# Preserve server status root
ProxyPass /server-status !
ProxyPass /rest http://localhost:8101/rest
ProxyPassReverse /rest http://localhost:8101/rest
<LocationMatch "^/rest">
SSLOptions +StdEnvVars
SSLRequire %{SSL_CLIENT_S_DN_CN} !~ m#(^testuser$)#i
SSLVerifyClient require
SSLVerifyDepth 2
RequestHeader unset Authorization
RequestHeader set user_id %{SSL_CLIENT_S_DN_CN}s
</LocationMatch>
<Directory />
Order Deny,Allow
Deny from all
Options None
AllowOverride None
</Directory>
</VirtualHost>
I am trying to redirect all the user requests to a different URL if the URL does not end with the dashboard. Below is the RedirectMatch directive and I tested the regex with different URL patterns online regex validator sites and all sites show regex is correct.
But for some reason, redirection is not happening.
RedirectMatch 301 ^/^((?!dashboard).)*$ https://abc.xyz.com/$1
Please let me know whether I am missing some in the Regex.
Apache config file
<VirtualHost *:443>
ServerName staging.abc.com
## Logging
LogLevel info
CustomLog "/var/log/apache2/abc_https_access.log" combined
ErrorLog "/var/log/apache2/abc_https_error.log"
ServerSignature Off
SSLEngine on
SSLProtocol -All +TLSv1.2
SSLCertificateFile /root/san/staging.abc.cer
SSLCertificateKeyFile /root/san/staging.abc.key
SSLCACertificateFile /root/san/abc.chain.cer
SSLVerifyClient require
RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1
## Proxy rules
ProxyRequests Off
ProxyPreserveHost On
# Preserve server status root
ProxyPass /server-status !
ProxyPass /rest http://localhost:8101/rest
ProxyPassReverse /rest http://localhost:8101/rest
<LocationMatch "^/rest">
SSLOptions +StdEnvVars
SSLRequire %{SSL_CLIENT_S_DN_CN} !~ m#(^testuser$)#i
SSLVerifyClient require
SSLVerifyDepth 2
RequestHeader unset Authorization
RequestHeader set user_id %{SSL_CLIENT_S_DN_CN}s
</LocationMatch>
<Directory />
Order Deny,Allow
Deny from all
Options None
AllowOverride None
</Directory>
</VirtualHost>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此重定向规则:
(?!.*dashboard$)
是一个否定的前瞻,如果 URL 以dashboard
结尾,则匹配失败。You can use this redirect rule:
(?!.*dashboard$)
is a negative lookahead to fail the match if URL ends withdashboard
.