有没有办法将这些重定向?没有办法、重定向

2023-09-02 09:46:10 作者:大崎娜娜.

我有两个重定向(一个用于HTTP,一个用于HTTPS):

I have two redirects (one for http and one for https):

# Option 1a: rewrite www.example.com → example.com

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

# Option 1b: rewrite https://www.example.com → https://example.com

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

是否有可能将它们合并,并删除的子站点的HTTP和HTTPS有一个规则集(无需指定确切域,像现在的规则)?

Is it possible to combine them, and remove the subdomain for both http and https with one ruleset (without having to specify the exact domain, like the current rules)?

推荐答案

下面是你可以两个规则组合成一个没有硬编码主机名:

Here is how you can combine both rules into one without hardcoding hostname:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www.([^#]+)#(?:off|on(s)) [NC]
    RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
</IfModule>