【问题标题】:How to redirect subdomains to index.php and keep subdomain url visible in address bar?如何将子域重定向到 index.php 并使子域 url 在地址栏中可见?
【发布时间】:2012-07-11 19:16:59
【问题描述】:

我有一个包含多个域的网站,例如 sub1.mydomain.orgsub2.mydomain.org

目前我有这个 htaccess 代码将所有这些子域重定向到 index.php 并将子域名作为变量传递:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ http://mydomain.org/index.php?subdomain=$1 [QSA,L]

一切正常,除了 URL 地址更改为mydomain.org/index.php?subdomain=(sub accessed)

我想保持子域名的显示。我该怎么做?

我的共享主机站点结构为

/username/public_html/addon-domain/index.php
/username/public_html/addon-domain/subdomains
/username/public_html/addon-domain/subdomains/sub1
/username/public_html/addon-domain/subdomains/sub2...

上面的htaccess文件在subdomains目录下。

编辑:----- 上述问题已解决,但出现了新情况 --------

我的 HTACCESS 代码的问题是我使用了绝对路径 (http://www.mydomain...),这将始终导致显示新的 url,无论 [R] 与否。相反,您应该使用相对路径。 但是,仍然存在一个令人困惑的问题。这是新代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.org$
RewriteRule ^(.*)$ (can't use relative here) [QSA,L]
RewriteRule ^(.*)$ \.\./index.php?subdomain=$1 [QSA,L]

第一个条件/规则将检查我的网站是否正在从子域访问,例如 sub.mydomain.org。如果它是真的,[L] 将停止第二个重写器运行。第二个重写器使用来自位于 /subdomains/ 文件夹中的 HTACCESS 文件的相对路径,如果我从 mydomain.org/subdomains/anysub 进入我的站点,则一切正常。

但是,如果站点输入为 anysub.domain.org,则相对路径不起作用。它将触发两个错误:“内部服务器错误。服务器遇到内部错误或配置错误,无法完成您的请求。”和“此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。”

有人会碰巧知道为什么吗?相对路径不应该总是与HTACCESS文件所在的位置相关吗?

编辑:刚刚发现 mydomain/subdomains/anysub/ 有效,但 mydomain/subdomains/anysub (不以 / 结尾)将再次触发显示 get var。开始认为这太复杂了,无法得到答案

【问题讨论】:

标签: php .htaccess


【解决方案1】:

尝试将P 标志添加到RewriteRule 的括号中:

RewriteRule ^(.*)$ http://mydomain.org/index.php?subdomain=$1 [P,QSA,L]

【讨论】:

  • 添加 [P] 导致站点返回“Request-URI Too Large。请求的 URL 的长度超过了此服务器的容量限制。”直接访问 subdomain.domain.org 时。
猜你喜欢
  • 1970-01-01
  • 2019-08-23
  • 2016-09-23
  • 1970-01-01
  • 2018-10-03
  • 2011-06-19
  • 2012-01-27
  • 2018-08-31
  • 2013-05-21
相关资源
最近更新 更多