且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

国防部重写子域到PHP中GET变量

更新时间:2023-02-24 10:18:46

您必须捕获一个的 的RewriteCond 然后用此在的 重写规则 。附加的RewriteCond 是那里prevent www.mydomain.com 指数.PHP 被改写

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST}!^ WWW \。
的RewriteCond%{REQUEST_URI}!指数\ .PHP
的RewriteCond%{HTTP_HOST} ^(。+?)\。MYDOMAIN \ .COM $
重写规则。* /index.php?username=%1 [L]
 

但是,这是所需要的功能的一小部分。此外,您必须对您的所有 username.mydomain.com 域名或设置一个的通配符DNS 作品 *。mydomain.com 指向您的主机。

否则,客户端尝试联系 jcraine.mydomain.com ,例如,而没有找到DNS条目和抱怨。

如果这是一个虚拟主机,还必须添加一个 ServerAlias​​ 为每个用户名

  ServerAlias​​ jcraine.mydomain.com
 

或通配符捕获所有子

  ServerAlias​​ * .mydomain.com
 

I'm trying to wrap my head around mod rewrite, but can't seem to figure this out.

Is there a way I could do the following?

User types in: http://wildcard.mydomain.com

Silently writes to:

http://mydomain.com/index.php?username=wildcard

You must capture the first part of the domain in a RewriteCond and then use this in a RewriteRule. The additional RewriteConds are there to prevent www.mydomain.com and index.php being rewritten

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$
RewriteRule .* /index.php?username=%1 [L]

But this is only a small part of the needed functionality. Additionally, you must setup DNS entries for all of your username.mydomain.com domain names or setup a wildcard DNS entry *.mydomain.com pointing to your host.

Otherwise, the client tries to contact jcraine.mydomain.com, for example, and doesn't find a DNS entry and complains.

If this is a virtual host, you must also add a ServerAlias for each of your usernames

ServerAlias jcraine.mydomain.com

or a wildcard catching all subdomains

ServerAlias *.mydomain.com