Forum Moderators: phranque

Message Too Old, No Replies

Dynamic subdomain rewrite

Similar to WP's permalinks, but w/ subdomains and folders

         

twprogrammers

11:30 pm on Aug 3, 2014 (gmt 0)

10+ Year Member



I want to do something similar to WordPress's permalinks, but rather that rewriting all non-existing files and folders to index.php, rewrite subdomains to folders on a separate domain. An example of what I want is:
  1. User goes to http://osd1.sd1.msd.example.com
  2. Server checks if /var/www/example.com/sc/msd/sd/sd1/osd/osd1 exists
    • If yes: show /var/www/example.com/sc/msd/sd/sd1/osd/osd1
    • If no: redirect user to http://msd.example.com (which displays /var/www/example.com/sc/msd)



Basically, I am trying to create a dynamic game site setup where you go to {corp_ticker}.{alliance_ticker}.eve.example.com. I really don't want to manually add everything, so I am looking at making it dynamic with mod_rewrite. I tried looking it up, but my Google fu is still in padawan level :-P so if it has been answer somewhere before I am sorry. I was going to try adapting the WordPress htaccess file and stuff I found in my search to what I am trying to do, but I can't seem to grasp the mod_rewrite stuff enough to adapt it to my needs like I do with other code.
I hope I made sense there... If you need clarification let me know and I will try to clarify.

[edited by: incrediBILL at 11:54 pm (utc) on Aug 3, 2014]
[edit reason] unlinked URL [/edit]

phranque

8:25 am on Aug 4, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



for one thing you will need a pattern that will capture the backreferences from the hostname (osd1.sd1.msd.example.com) that you need to test the file path name (/var/www/example.com/sc/msd/sd/sd1/osd/osd1)

twprogrammers

12:10 pm on Aug 4, 2014 (gmt 0)

10+ Year Member



OK... Can I get example code? Or a howto link? I gathered what I had to do, just not the how. If it were PHP it would be extremely easy, just a little regex matching and test the location. Hmm... would I be able to have a PHP script server-side redirect? Like rewrite it all to a PHP file that shows the proper directory? The way WP does it though is it just echos the corresponding parameters on the page, right? So just copying the whole htaccess from my WP directory into the Apache site configuration...

wilderness

2:17 pm on Aug 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FWIW, How to Speed Up WordPress [webmasterworld.com] leading all the threads, should be useful for the syntax.

twprogrammers

6:51 pm on Aug 20, 2014 (gmt 0)

10+ Year Member



Anybody?

lucy24

8:09 pm on Aug 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It sounds as if you're talking about a more narrowly constrained version of

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.+?(?:/|\.php))$ http://www.example.org/$1 [R=301,L]

where example.org is the other site, and ".php" is a stand-in for whatever extension you actually do use. (The pattern will look a little different if you are extensionless.)

It's crucial to word the rule so it only applies to requests for pages-- hence the final / or .php in the body of the rule --because nothing is more resource-intensive than the -f check. When the request is for a non-page file, you already know that nobody is going to get redirected.

For the same reason, if the rule is only to be invoked within a particular subdomain, include that subdomain as a RewriteCond looking at %{HTTP_HOST}. Eliminate everything possible before you do the -f test.

In fact this is the rare case where it may be appropriate to add another htaccess, including RewriteRules, in the physical directory that contains the files you're concerned with. Requests on other paths simply won't see the rule. If so, be sure to add the line
RewriteOptions inherit

twprogrammers

5:06 am on Aug 21, 2014 (gmt 0)

10+ Year Member



@lucy24
Well... not exactly. I don't want a redirect. I want the URL in the address bar to stay cticker.aticker.eve.example.com, unless /var/WWW/example.com/sc/eve/aticker/cticker doesn't exist. Normally I would use this:
<VirtualHost *:80>
ServerName cticker.aticker.eve.example.com
DocumentRoot /var/www/example.com/sc/eve/aticker/cticker
...
</VirtualHost>

However, this is not dynamic and thus not automatic.

lucy24

6:00 am on Aug 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't want a redirect. I want the URL in the address bar to stay cticker.aticker.eve.example.com, unless /var/WWW/example.com/sc/eve/aticker/cticker doesn't exist.

So, uhm, you do want a redirect-- but only when the requested file doesn't exist. That's why you have to do the existence check, though it may be a little more complicated than -f in your case.

twprogrammers

3:56 pm on Aug 21, 2014 (gmt 0)

10+ Year Member



RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.+?(?:/|\.php))$ http://www.example.org/$1 [R=301,L]


This would redirect to a different place, would it not?

lucy24

7:54 pm on Aug 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Anything with the [R] flag and/or with target giving a full protocol-plus-domain will be a redirect.

My reading of your prose is
I want the URL in the address bar to stay [unchanged],
unless
{blahblah}

=
I want the URL in the address bar to change
if
{blahblah}

There is undoubtedly a way to express that in formal logical symbolic notation, but I just do Regular Expressions.