【问题标题】:Catching the domain name with .htaccess使用 .htaccess 捕获域名
【发布时间】:2020-06-10 20:47:19
【问题描述】:

我对使用 .htaccess 和 modrewrite 还是很陌生。

我有一个php的多租户网站,每个租户都可以在指向我的服务器ip后将他购买的域名保存在应用程序数据库中。

现在我正在尝试通过 .htaccess 编写重写规则,以便当用户在地址栏中(例如 www.example.com)键入他的域时,.htaccess 解析主机名并将其作为参数传递给索引页面,这样对 www.example.com 的每个请求都会变成这样: /index.php?domain=www.example.com 。

甚至 www.example.com/user 变成 /index.php?domain=www.example.com&url=user

如何使用 .htaccess 来实现?

感谢您的帮助!

【问题讨论】:

    标签: php .htaccess hostname


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用此规则:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} !domain= [NC]
    RewriteRule ^(?:index\.php)?$ index.php?domain=%{HTTP_HOST} [L,QSA,NC]
    
    RewriteCond %{QUERY_STRING} !domain= [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php?domain=%{HTTP_HOST}&url=%{REQUEST_URI} [L,QSA]
    

    【讨论】:

    • 太棒了!如果我想让 www.example.com/user 变成 index.php?domain=www.example.com&url=user 怎么办?谢谢!
    • 好的,现在检查我的更新答案,但请避免通过 cmets 添加要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多