Reputation: 11
Alrighty. My first question on SO after a couple years of lurking and being thankful for folks willing to help, so thank you for your patience in advance with my newbitude and inaccurate terminology.
I inherited a job and a project where users get their own subdomain with a set of 4-5 template php pages with semi-customizable content stored in a mysql database. It supposedly working before it came to me, now it doesn't, so I'm trying to debug the whole thing as I have to duplicate the project for another domain, so I come to you, my gurus, after several hours of research, dead-ends, and half-truths from the interweb.
I've been through the IIS URL Rewrite page, looked around here and elsewhere, but I'm just getting confused.
I'm trying to accomplish the following for a growing number of users:
bubba.example.com ===> example.com/users/index.php?c=bubba
bubba.example.com/about.php ===> example.com/users/about.php?c=bubba bubba.example.com/contact.php ===> example.com/users/contact.php?c=bubba
and so on... but I get 404 errors every time, regardless of the username/subdomain name or the template site. Some background info:
I've had several versions of this first rule, via web.config and the IIS panel, and this is just the latest version, which I know doesn't work:
<rule name="User Sites" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(\w+)\.(example\.com)$" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Rewrite" url="http://(C:1)/users/{R:0}?c={C:0}" logRewrittenUrl="true" />
</rule>
My next step was to set up rules for each template page, something like this (might not have the regexes right, but you get the idea:
<rule name="User about page" stopProcessing="true">
<match url="^about\.php$" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(\w+)\.(example\.com)$" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Rewrite" url="http://(C:1)/users/about.php?c={C:0}" logRewrittenUrl="true" />
</rule>
Some help would be appreciated, even a push in the right direction
EDIT:
ok so now it's really a crunch time and still no definitive answer.
here's my latest rule, but it's still not working...
<rule name="rewrite">
<match url="(.*)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.(.*\.c.*)$" />
</conditions>
<action type="Rewrite" url="http://www.{C:3}/users/{R:0}\?c={C:1}" appendQueryString="true"
</rule>
I realize this is overkill, as it would not be limited to just the 2 domains I want to rewrite, but I need something functional before I can refine, and this is still not working right.
Upvotes: 0
Views: 1671
Reputation: 11
Ok. It turned out that there were other inbound and outbound rewrite rules tucked into the applicationHost.config and the individual web.config files for each domain that did not populate in the respective URL Rewrite modules. perhaps a farewell gift from my predecessor? I'll never know.
In case some other inexperienced web admin like me runs into this in their own projects, here's what I had to do :
- server's IP: 123.456.789.0 (for this example, I know it's not valid)
- DNS A records: * => 123.456.789.0 , @ =>123.456.789.0 (both domains have these same A records.)
default webpage
- root directory: %SystemDrive%\inetpub\wwwroot
- bindings: protocol="http" bindingInformation="*:80:"
Example1.com
- root directory: %SystemDrive%\inetpub\wwwroot\example1
- bindings:
- protocol="http" bindingInformation="*:80:www.example1.com"
- protocol="http" bindingInformation="*:80:example.com"
Example2.com
- root directory: %SystemDrive%\inetpub\wwwroot\example2
- bindings:
- protocol="http" bindingInformation="*:80:www.example2.com"
- protocol="http" bindingInformation="*:80:example.com"
Rewrite rule looks like this:
<rewrite>
<rules>
<rule name="rewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)\.example1\.com$" />
</conditions>
<action type="Rewrite" url="example2/users/{R:0}?c={C:1}" appendQueryString="true" />
</rule>
<rule name="rewrite2" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)\.example2\.com$" />
</conditions>
<action type="Rewrite" url="example2/users/{R:0}?c={C:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
The default website's rewrite configuration acts as a "router" of sorts for our user pages for these 2 domains.
for those not as fluent in regex,
- "^(?!www)" = the string cannot start with the literal letters "www"
- "(.*)" = any group of characters, but not optional. the parantheses capture it for back reference
- ".example1.com$" = the remainder of the host must be [dot]example1[dot]com, since the "$" indicates the end of the string.
I still need to tweak our canonization for SEO, so I'll also have to work in our canonization rewrites, which includes one site registered as both ".com" and a ".ca", but at least I'm on my way.
I'm sure there is a prettier way of doing this, but this works for now.
Hope this helps someone else trying to do this in the future.
C.
Upvotes: 0