apache - 如何为特定匹配 https 和 www 一起创建 RewriteRule?

标签 apache ssl url-rewriting virtualhost

我已经设法为 www 和非 www(纯 http)和 https 创建了有效的重定向,但是当它们一起使用时,它就不再起作用了。我有四个域,希望每个域都重定向到一个基本域,然后添加 url 参数。

现在尝试不同变体时的当前结果:

www.example.fi -> redirects me to https://example.se/?lang=fi. OK
http://example.fi -> redirects me to https://example.se/?lang=fi OK
https://example.fi -> redirects me to https://example.se/?lang=fi OK
https://www.example.fi -> redirects me to https://example.se without the lang parameters.  WRONG. It should take me to the same place as the other.

这是我的 http 虚拟主机:

<VirtualHost *:80>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://example.se/?lang=fi
</VirtualHost>

这是我的 https:

<VirtualHost *:443>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /home/user/example.fi.pem
SSLCertificateKeyFile /home/user/example.fi.key
SSLCACertificateFile /home/user/intermediate.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

如您所见,我希望不同的语言域使用语言参数指向 SE 域。

如何修改我的代码,使其与 https、http 和 www 的所有可能变体一起匹配?

最佳答案

在你的代码中

  RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
  RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

重写仅匹配 htt(s)://exemple.fi URL

没有 ^ 它应该可以工作

  RewriteCond %{HTTP_HOST} example\.fi$ [NC]
  RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

关于apache - 如何为特定匹配 https 和 www 一起创建 RewriteRule?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56167475/

相关文章:

Apache 重写规则不适用于 angularjs

php - 如何使用 PHP 和 mySql 为用户配置文件创建单独的 Web 页面?网址重写?

php - 从 PHP 脚本执行 shell 脚本

java - 在 apache/tomcat 设置 (mod_jk) 后面的 Vaadin 中获取 IP 地址总是给出服务器的 IP 地址

rest - 使用 Heroku 和 303 的 SSL 端点

java - 了解 SSL 信任策略

mod-rewrite - mod_rewrite 将多个请求发送回 Lucee 服务器

apache - 如何使用 Apache 2.4 和 mod_ssl 启用 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 密码

linux - mod_rewrite 不适用于子域

java - HermesJMS 显示错误 :javax. jms.JMSException : Could not create Transport. 原因 : java. io.IOException:无法加载资源: