Apache 重写 URI,捕获 pages/folders
Apache rewrite for URI, capture pages/folders
我正在尝试完成以下...如果用户通过
123abc.com(带或不带 www) 和 123abc.com/hello(带或不带 www) 它们应该自动带到 https://www.123abc.com/URI_IF_NEED
...
这就是我目前所拥有的。基本域重定向正确,但任何 URL 结构后跟 pages/dir 都不会。
努力实现https://www.123abc.comand/or
https://www.123abc.com/hello
ServerName www.123abc.com
ServerAlias 123abc.com
DocumentRoot "/mnt/var/www/html/"
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
您不需要为此进行复杂的重写。只需创建不同的虚拟服务器,然后让 non-www
在 www
(443/https) 上重定向,并在 (80/http)
上重定向一个
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# real server configuration
</VirtualHost>
PS:积分 apache redirect from non www to www
如果你还想像以前那样写,那么你可以像下面这样写
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
在线测试仪上测试
https://htaccess.madewithlove.be/
不同的测试结果
我正在尝试完成以下...如果用户通过
123abc.com(带或不带 www) 和 123abc.com/hello(带或不带 www) 它们应该自动带到 https://www.123abc.com/URI_IF_NEED
...
这就是我目前所拥有的。基本域重定向正确,但任何 URL 结构后跟 pages/dir 都不会。
努力实现https://www.123abc.comand/or https://www.123abc.com/hello
ServerName www.123abc.com
ServerAlias 123abc.com
DocumentRoot "/mnt/var/www/html/"
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
您不需要为此进行复杂的重写。只需创建不同的虚拟服务器,然后让 non-www
在 www
(443/https) 上重定向,并在 (80/http)
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# real server configuration
</VirtualHost>
PS:积分 apache redirect from non www to www
如果你还想像以前那样写,那么你可以像下面这样写
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
在线测试仪上测试
https://htaccess.madewithlove.be/
不同的测试结果