Wildcard Subdomains/URL Rewriting

Ok say what I have done was setup a wildcard subdomain so anything.xugogaming.com will go to the main site in which my existing rewrite modules will serve any fancy pages I’ve associated with it. So that part is setup correctly.

However, what I would like to do is if there’s a specific pattern, like a name, jay-z.xugogaming.com, I would like to to go to the index.php of a folder inside my root directory called authorblog which has a wordpress installation there which I will setup with the MU (multi-user) features. But I don’t to direct every request from jay-z.xugogaming.com into that index.php, if there is a request for image or a file I’d like to load that instead so it all looks and works like a normal/standard setup.

In short it would be setup like this (I think):

jay-z.xugogaming.com/ -> /authorblog/index.php
jay-z.xugogaming.com/wp-admin/css/install.css -> /authorblog/wp-admin/css/install.css

I thought it would be simple enough by using:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugogaming.com
RewriteRule (^[-a-z0-9/.]+$) /authorblog/$1 [L]

Which handles basic folder names, file names and such. But for some reason that gives me a 500 Internal Server error when that condition is met and the redirect should occur.

What should happen is:

jay-z.xugogaming.com/wp-admin/css/insall.css -> /authorblog/wp-admin/css/install.css

but again I just get that 500 Internal Server error.

Also, what sort of works was

RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugogaming.com
RewriteRule (.*) /authorblog/index.php [L]

Which showed the right page and all, but the css,images, and other media does not work.

Any ideas how I can get this to work? I’m assuming it’s possible, it seems like it should be.

No worries, mate!

Regards,

DK

JZ,

The .? is really just a placeholder (zero or one of any character in the {REQUEST_URI} string) and is not any more useful than that (because it’ll match anything and enable the redirection).

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugostudios.com
# {HTTP_HOST} should match one or more lowercase letter
# followed by a dash and a single lower case letter then
# [COLOR="Red"]ONE OF ANY CHARACTER[/COLOR]
# followed by xugostudios followed by
# [COLOR="Red"]ONE OF ANY CHARACTER[/COLOR]
# followed by com
# [COLOR="Red"]and anything else that may come along[/COLOR]
# The problems here are (1) you're not capturing the
# subdomain string parts (unless you really don't need them),
# (2) that you're not escaping the dot characters and
# (3) you're not trying to match the end anchor
# (which would allow a convoluted subdirectory string to match)
RewriteRule ([^/]) authorblog/$1
# That's trying to match ONE of any character except / and
# redirecting to authorblog/{ONE character}

No, I agree that it makes sense that it wouldn’t work for the reasons I’ve commented on above.

No, as explained, it won’t even do that!

Okay, it’s been so long that I’ve forgotten what we went through to get to this point but I feel like we’re going in circles. At least it seems that you have the missing support file problem straightened out!

Regards,

DK

That’s where I got the (.?) from which I didn’t think it was right but wondered if somehow that was what I was looking for.

I’m trying to use your suggestion so I have this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugostudios.com
RewriteRule ([^/]) authorblog/$1

But that doesn’t seem to work and it makes sense that it wouldn’t. If the requested URI is /wp-admin/wp-config.php then all it’s going to pick up is the wp-config.php and attempt to load /authorblog/wp-config.php

So far this works about right, the index.php from the authorblog folder is loaded when the root of the xugostudios.com domain is requested. Now it’s just a matter of figuring out how to redirect the individual file requests.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugostudios.com
RewriteRule ^$ authorblog/index.php

Thanks very much! Ok, I see. Is {REQUEST_URI} a variable you can use? Or am I supposed to replace that with something else?

I learned that you can’t use a variable like $1 from a sub-pattern if you’re just trying to serve a file from the filesystem rather than upload the url. At least that seems to be what keeps throwing the error.

Though I did finally get it, thanks to your advice, to serve the wordpress index when you go to jay-z.xugogaming.com/ however, now I gotta add another chain rule or something for the regular files so if it goes to jay-z.xugogaming.com/wp-admin it shows the contents of that folder in my authorblog folder in my root directory.

Would making a more visual chart of my file system help explain my problem better?

/ (root) (xugogaming.com)

| .htaccess (the one this topic is about)
| index.html
| images/
| authorblog/
     |-> <wordpress files here>

And again I’d like it so that in the .htaccess it says if the subdomain matches the pattern in the subdomain, like jay-z.xugogaming.com, to load the files and content from the authorblog folder. But I would like to completely avoid from ever having the authorblog/ folder come up in a url or something unless manually typed in of course so it has the appearance of completely running from the root of jay-z.xugogaming.com.

JZ,

{REQUEST_URI} is an Apache variable which can be addressed in mod_rewrite by using a % prefix, i.e., %{REQUEST_URI}. This is the ONLY variable that is examined for a match by a RewriteRule.

I’m not clear on what you’re after with

I think the remainder of you post can be answered by saying that subdomains are treated as completely separate from the domain request so that, if your subdomain’s DocumentRoot is a subdirectory of the main domain, it cannot access domain subdirectories (other than its own) as they are outside it’s webspace. If, however, your subdomains are co-located with the main domain, then they also “own” the domain’s webspace and all files are available.

The problem in the second case is that you must redirect EVERYTHING other than existing files (in other subdirectories) to the WP subdirectory. That’s already handled by WP’s mod_rewrite EXCEPT that it thinks that index.php is located in the DocumentRoot (you can alter its code to WP/index.php - but that may upset its internal links to supporting modules). Try it and let us know, please.

Regards,

DK

Jay,

Yes, it IS possible.

It generally helps to see ALL your .htaccess as the order of mod_rewrite code IS critical, however, what you’ve shown is enough to point out a few problems:

  1. If I were the regex engine, I’d leave it to the parser to return the 500 code for your lack of the closing ) before the END anchor (the $). Including the closing ) AFTER the END anchor would only have upset it that much more!
RewriteEngine On
RewriteCond &#37;{HTTP_HOST} ^[a-z]+-[a-z].xugogaming.com
RewriteRule (^[-a-z0-9/.]+$[COLOR="Red"])[/COLOR] [COLOR="Red"]/[/COLOR]authorblog/$1 [L]
  1. Omitting the escape characters in the regex of the RewriteCond is not a “game stopper” but it is incorrect if you’re intending to REQUIRE the dot character rather than the metacharacter (representing ANY character).

  2. THEN, using the / before authorblog in the redirection can often lead to problems as mod_rewrite looks to the physical path on the system BEFORE the {DocumentRoot} path.

  3. Finally, your RewriteRule will cause a loop (because it includes EVERYTHING in authorblog/$1. This can easily be eliminated by adding a RewriteCond to look for authorblog/ in the {REQUEST_URI} so I won’t dwell on this.

  4. When you get to the :kaioken: EVERYTHING :kaioken: atom, though, you have this same problem again.

RewriteCond %{HTTP_HOST} ^[a-z]+-[a-z].xugogaming.com
RewriteRule [COLOR="Red"](.*)[/COLOR] [COLOR="Red"]/[/COLOR]authorblog/index.php [L]

I will typically replace the (.*) with regex similar to what you have above (without the /) OR use .? (when I don’t need the {REQUEST_URI} because I already HAVE the {REQUEST_URI}, i.e., RewriteRule .? path{REQUEST_URI} [L] where the {REQUEST_URI} already contains the / after the domain name).

There’s a LOT of information there so take your time to digest it - then come back with questions.

Regards,

DK

I did more research on the multi-user wordpress installation and it turns out it would only have worked if it’s in the root directory of the wildcard sub domain, which it’s not. However, I can setup a specific blogs.domain.com and use a blogs.domain.com/username type of system which seems like my only option for this really.

Thanks again for your help, and I apologize for wasting your time when what I wanted couldn’t have happened anyways.

JZ,

Yes, unfortunately that’s more clear. However, what your code is saying is to capture ZERO OR ONE of ANY CHARACTER and redirect to that character in the authorblog directory. Somehow, I can’t believe that’s what you’re asking for. Perhaps ([-a-zA-Z_.]+) or ([^/]+) would work better for you (the first will match one or more letter, hyphen, underscore or period characters whereas the second will match one or more of EVERY character EXCEPT the / - designed to prevent authorblog/whatever from looping to authorblog/authorblog/whatever). The key here is to know what you can match and make sure that it can’t loop.

Okay, the wildcard subdomains share the main domain’s DocumentRoot. That will mean that they can access the same webspace, however, because the authorblog files are in a subdirectory, you must be very careful and be sure that the files know what directory level they should direct the supporting file requests. Poor wording, excuse me. What I’m trying to say is that, using the mod_rewrite to request files from authorblog/ (hidden) means that the browser will assume it’s in the DocumentRoot and all relative links should be relative to the DocumentRoot (I think that the PHP includes will be okay relative to the authorblog directory, the location of the index.php file).

While the thanks are appreciated, what I am trying to do is help members learn - so please keep asking questions until everything is clear.

Regards,

DK

Oh! I did use the completely wrong word let me rephrase that:

I learned that in that one pattern I was using

RewriteRule (.?) authorblog/$1

While I’m expecting a url like http://jay-z.xugogaming.com/wp-admin to load the contents of http://jay-z.xugogaming.com/authorblog/wp-admin. However, apache doesn’t seem to want to load files based on the sub-pattern variables like the $1 matched from the (.?).

Is that more clear?

That’s the way I understand subdomains to work, and my wildcard subdomain is pointed to our root directory the mainsite is pointed out of. However I want anything that matches a pattern like http://jay-z.xugogaming.com/ to serve the contents of the authorblog folder which contains the wordpress files without having to use that extra authorblog folder in the url. I know it will always be able to be accessed like that, but I would like to give the impression that those subdomains go to their own root folder by rewriting the url’s in them to the authorblog folder files behind the scenes.

Is that helping any?

Thanks again for your help. I’m really grateful :slight_smile: