apache - 如何获取htaccess中的子域名和域名

标签 apache .htaccess subdomain

例如,我有 URL subdomain.example.com

如何从 .htaccess 中的 URL 获取子域

我需要做这样的事情:

RewriteRule (.*) example.com/subdomain/index.php

我想出了一个子域,但对于仍然没有运气。

RewriteCond %{HTTP_HOST} ^(^.*)\.example.com
RewriteRule (.*) example.com/%1/index.php

最佳答案

RewriteCond %{HTTP_HOST} ^(^.*)\.example.com
RewriteRule (.*) example.com/%1/index.php

同样的原则也适用于域(尽管您在此处使用的CondPattern似乎无效?)。看起来您需要域名+TLD(基本上是子域名之后的所有内容),因此请尝试以下操作:

RewriteCond %{HTTP_HOST} ^(.+?)\.(.+?)\.?$
RewriteRule ![^/]+/[^/]+/index\.php$ %2/%1/index.php [L]

(.+?) CondPattern 的一部分获取子域(? 使其成为非贪婪的)。 (.+?)部分获取域(主机的剩余部分),这也是非贪婪的,以避免匹配完全限定域上的可选尾随点(例如 subdomain.example.com. )。

RewriteRule 模式 ![^/]+/[^/]+/index\.php$仅当 URL 不是 /<something>/<something>/index.php 形式时才进行重写,从而防止重写循环(500 错误) .

关于apache - 如何获取htaccess中的子域名和域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43296201/

相关文章:

apache .htaccess重写-我可以将其移到httpd.conf中吗

php - 在 WordPress 中使用 PHP 更改 CSS 样式

redirect - 每个子域和根重定向一个 SSL 证书

.htaccess 将index.php重定向到home而不重定向子域

heroku - 我可以通过浏览器访问我的 heroku 免费子域站点,但我无法在终端中 ping 到它

php - 无法使用 PHP 5.5 在 Ubuntu 14.04 上的 Laravel 4.2 中获得任何路由

php - apache 用户无法运行 sox 命令

ajax - 使用文件夹名称填充 JSP 下拉列表

apache - 如何为 Apache httpd 指定 "TLSv1.2 protocol or later"

.htaccess 规则将图像文件的直接 View 重定向到页面,图像作为查询字符串传递