【问题标题】:Using Wild Card Subdomains With Mod Rewrite?使用带有 Mod_rewrite 的通配符子域?
【发布时间】:2010-09-18 17:08:52
【问题描述】:

我有通配符子域,但是我只是不知道 mod_rewrite 到需要写这个的程度。谁能告诉我如何做到这一点,除了 www 以外的任何东西都不会进入主站点,但除此之外的任何子域都进入 /script/index.php?username=$username?

【问题讨论】:

  • 您的意思是“谁能告诉我如何使 www 没有任何东西进入主站点...”?

标签: mod-rewrite subdomain wildcard-subdomain


【解决方案1】:

$username 变量应该来自哪里??

假设主站点的 URL 是 http://www.example.com/main_site.php,并且您正在使用这个外部目录上下文(即,不在 .htaccess 或 指令中)。如果它在 .htaccess 中,则删除前导 /(例如,将其设为 main_site.php)。

我认为这不会立即起作用,因为有许多不明确的变量(用户名来自哪里?,如何处理请求的其余部分,将其作为参数传递?,这是 htaccess 还是主配置? ),但希望能给你一个想法:

#Turn on the rewrite engine
RewriteEngine On
#Check accessed domain, if it's either www.example.com or
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC,OR]
#example.com
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
#and the requested URL does not contain script you'll be accessing to avoid looping
RewriteCond %{REQUEST_URI} !main_site.php
#Then we tell that everything matching the above will go to main_site.php
RewriteRule ^ /main_site.php [L]
#If the request is not asking for main_site.php nor index.php
RewriteCond %{REQUEST_URI} !main_site.php
RewriteCond %{REQUEST_URI} !index.php
#We go to /script/index.php (username will be empty, becase we don't know 
#where to get it from)
RewriteRule ^ /script/index.php?username=$username [L]

【讨论】:

  • 用户名来自子域。
  • 如果它什么都不是(即,只是 domain.com)怎么办?
猜你喜欢
  • 2013-07-18
  • 1970-01-01
  • 2014-04-14
  • 2023-04-09
  • 2019-11-12
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多