通配符.htaccess重写子域和白标签域作为GET变量



首先,我不擅长重写规则。我已经讲过了通配符。htaccess重写子域到子域作为GET变量的子目录(我的问题)和。htaccess重写:子域作为GET参数和文件路径后域完整和https://serverfault.com/questions/214512/,但我找不到解决我的问题的方法。我也不确定我的要求是否可能,但我仍然要求确保。
期望结果:

http://example.com             -> https://www.example.com             [redirect]
http://example.com/dir/?key=12 -> https://www.example.com/dir/?key=12 [redirect]
https://example.com            -> https://www.example.com             [redirect]
https://example.com/dir/?key=12 -> https://www.example.com/dir/?key=12 [redirect]
http://xyz.example.com         -> https://xyz.example.com             [redirect]
http://www.xyz.example.com     -> https://xyz.example.com             [redirect]
https://www.xyz.example.com    -> https://xyz.example.com             [redirect]
https://xyz.example.com        -> https://www.example.com/whitelabel/?user=xyz [proxy]
https://xyz.example.com/profile.php -> https://www.example.com/whitelabel/profile.php?user=xyz            [proxy]
https://xyz.example.com/dir/settings.php -> https://www.example.com/whitelabel/dir/settings.php?user=xyz  [proxy]
https://xyz.example.com/dir/settings.php?par=val -> https://www.example.com/whitelabel/dir/settings.php?user=xyz&par=val [proxy]
http://example.org             -> https://www.example.com/whitelabel/?domain=abc.example.org              [proxy]
https://www.example.org        -> https://www.example.com/whitelabel/?domain=example.org                  [proxy]
https://example.org?param=val  -> https://www.example.com/whitelabel/?domain=example.org&param=val        [proxy]
https://example.org/dir1/dir2?param=val -> https://www.example.com/whitelabel/dir1/dir2?domain=example.org&param=val [proxy]

为什么我需要这个?默认情况下,每个注册用户都有一个子域名,但如果他有一个域名,他可以将该域名指向我的专用IP。我必须为每个子域或域显示不同的内容。
我已经做了http->https,并在需要的地方添加www。我还传递了子域作为GET参数。每个代码单独工作得很好,但是组合起来就不工作了。也许,这与规则的顺序有关。总之,这是我的。htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^((?!www.)[^.]+).toours.com$
RewriteRule ^ https://www.toours.com/whitelabel%{REQUEST_URI}?user=%1 [QSA,L,P]
RewriteRule (.*)/whitelabel/(.*) $1/$2 [L]
</IfModule>

这应该能帮你完成大部分工作

<IfModule mod_rewrite.c>
RewriteEngine On
# ensure www when no subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule .* https://www.%0/$0 [L,R=301]
# ensure no www when user subdomain
RewriteCond %{HTTP_HOST} ^www.([^.]+.example.com)$
RewriteRule .* https://%1/$0 [L,R=301]
# ensure https for all (you will have to add users' own domains to your SSL cert)
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}/$0 [L,R=301]
# set env vars if user subdomain
RewriteCond %{HTTP_HOST} ^(?!www.)([^.]+).example.com$
RewriteRule ^ - [E=USERID:%1,E=USERPARAM:user]
# set env vars if user own domain
RewriteCond %{HTTP_HOST} ^.+(?<!.example.com)$
RewriteRule ^ - [E=USERID:%0,E=USERPARAM:domain]
# user main page pretty URLs (removed ".php" from the public URLs)
RewriteCond %{ENV:USERID} .+
RewriteRule ^$ whitelabel/?%{ENV:USERPARAM}=%0 [L,B,QSA]
RewriteCond %{ENV:USERID} .+
RewriteRule ^profile$ whitelabel/profile.php?%{ENV:USERPARAM}=%0 [L,B,QSA]
RewriteCond %{ENV:USERID} .+
RewriteRule ^dir/settings$ whitelabel/dir/settings.php?%{ENV:USERPARAM}=%0 [L,B,QSA]
RewriteCond %{ENV:USERID} .+
# put any other whitelabel rules before this, so any remaining URLs that are not on your main domain get a custom 404 page
RewriteRule ^ whitelabel/dir/404.php?%{ENV:USERPARAM}=%0 [L,B,QSA]
# place any other pretty URLs for main domain here, probably rewrite all non-file requests to a single file and route from there
</IfModule>

查询字符串将在默认情况下附加在替换没有得到的地方,并且我在替换有的地方添加了QSA

不确定如何重定向到规范用户拥有的域

最新更新