【问题标题】:Wildcard .htaccess rewrite subdomain and whitelabel domain as GET variable通配符 .htaccess 将子域和白标签域重写为 GET 变量
【发布时间】:2016-10-25 11:22:13
【问题描述】:

首先,我不擅长重写规则。我已经通过了 Wildcard .htaccess rewrite subdomian to a subdirectory with subdomain as GET variable(我的问题)和.htaccess rewrite: subdomain as GET parameter and filepath afterdomain intacthttps://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>

这只是一个内部服务器错误。

【问题讨论】:

  • 注释掉RewriteRule (.*)/whitelabel/(.*) $1/$2 [L]这一行并重新测试。
  • 还是同样的错误。感谢您加入@anubhava。我正在考虑通过链接与您联系。你的名声在你之前。
  • 谢谢。您应该检查 Apache error.log 以了解为什么此时会出现 500。

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


【解决方案1】:

这应该可以帮助您完成大部分工作

<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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多