通配符.htaccess将subdomian和whitelabel域重写为GET变量

时间:2016-10-25 11:22:14

标签: apache .htaccess mod-rewrite url-rewriting

首先,我对重写规则不满意。我已经完成了 Wildcard .htaccess rewrite subdomian to a subdirectory with subdomain as GET variable(我的问题)和.htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact以及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>

这只会产生内部服务器错误。

1 个答案:

答案 0 :(得分:0)

这应该可以帮到你的大部分时间

<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添加到其中。

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