subdomain rewrite & setting of environment variable

Using a single web hosting account to host multiple sites

Postby richardk » Wed May 20, 2009 5:13 pm

I checked it and no luck. If I set it to TEST:test and dump all the environment variables, the words TEST or test never show up

Where? The PHP $_SERVER array?

Try
Code: Select all
echo getenv('REDIRECT_TEST');


Is all my syntax correct here?

Looks fine to me.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Wed Jun 17, 2009 4:52 pm

Sorry I took so long to respond. I didn't notice the reply (was on page 2 of the thread).

It looks like it was working, however I was using Aliases as well, and so when an Alias is activated then redirect rules are never executed. There are some libraries which are shared by several sites. I would like to set this as an Alias, ie.

Alias /cms/ /usr/local/src/cms

However, when the CMS is accessed, the Modrewrite rules are never executed (as they have to be located within a <directory> tag, and the Alias directives must be outside of directory tags), therefore the server environment variable is never set. The code needs this variable in order to know which site is accessing it at any given time.

Any ideas how to get around this limitation? Is there any way to set a dynamic environment variable (ie. the 3rd element in the domain name <this>.domain.com) /outside/ of a directory tag so that it's set even if an alias takes precedence?

I really want to avoid using symlinks to link to the external code.

Thanks for any further help.
-Nick

richardk wrote:
I checked it and no luck. If I set it to TEST:test and dump all the environment variables, the words TEST or test never show up

Where? The PHP $_SERVER array?

Try
Code: Select all
echo getenv('REDIRECT_TEST');


Is all my syntax correct here?

Looks fine to me.
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Thu Jun 18, 2009 11:09 am

Post your current <VirtualHost>, <Directory> and mod_rewrite (all the relevant stuff).

The mod_rewrite might need the PT flag and/or moving to the <VirtualHost>.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Thu Jun 18, 2009 1:40 pm

Thanks a lot for your help, here it is:

Code: Select all
<VirtualHost *:80>
    ServerName sites.example.com:80
    ServerAdmin webmaster@example.com
    DocumentRoot /data/sites/sites.example.com/htdocs
    ErrorLog logs/sites.example.com-error_log
    CustomLog logs/sites.example.com-access_log common

    Alias /cms/ "/usr/local/staff/code/cms/"
    Alias /index.php "/usr/local/staff/code/index.php"

    <Directory /data/sites/sites.example.com/htdocs/>
        Options +FollowSymlinks -Indexes
        RewriteEngine on
        RewriteBase /

        # add slash to the URL subdir if there is none
        RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$    [NC]
        RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
        RewriteCond %{REQUEST_FILENAME} -d
        #RewriteCond %{REQUEST_URI} !/$
        RewriteRule ^clients/([^/]+)/(.*)$ http://%{HTTP_HOST}/$2index.php [L,R]

        # rewrites http://something.sites.example.com/ to http://sites.example.com/clients/something/
        RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$    [NC]
        RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
        RewriteRule !^clients/(.*)$ clients/%1%{REQUEST_URI} [QSA,E=TEST:%1,L]

    </Directory>

</VirtualHost>



richardk wrote:Post your current <VirtualHost>, <Directory> and mod_rewrite (all the relevant stuff).

The mod_rewrite might need the PT flag and/or moving to the <VirtualHost>.
[/code]
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Thu Jun 18, 2009 2:46 pm

Try
Code: Select all
<VirtualHost *:80>
    ServerName sites.example.com:80
    ServerAlias *.sites.example.com:80
    ServerAdmin webmaster@example.com
    DocumentRoot /data/sites/sites.example.com/htdocs
    ErrorLog logs/sites.example.com-error_log
    CustomLog logs/sites.example.com-access_log common

    Alias /cms/ "/usr/local/staff/code/cms/"
    Alias /index.php "/usr/local/staff/code/index.php"

    Options +FollowSymlinks -Indexes

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$    [NC]
    RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com)$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/clients/%1%{REQUEST_URI}/ -d
    RewriteRule ^/.*[^/]$ http://%{HTTP_HOST}$0/index.php [R=301,L]

    RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
    RewriteRule ^/.*$ /clients/%1$0 [QSA,E=TEST:%1,PT,L]
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Thu Jun 18, 2009 5:07 pm

I tried out your ruleset (thank you for taking the time to help me out this much!) and i can't figure out why, but the aliases don't seem to be taking. (in the apache logs it says):

Code: Select all
script '/data/sites/sites.example.com/htdocs/clients/<something>/index.php' not found or unable to stat


same goes for when I try /cms/

richardk wrote:Try
Code: Select all
<VirtualHost *:80>
    ServerName sites.example.com:80
    ServerAlias *.sites.example.com:80
    ServerAdmin webmaster@example.com
    DocumentRoot /data/sites/sites.example.com/htdocs
    ErrorLog logs/sites.example.com-error_log
    CustomLog logs/sites.example.com-access_log common

    Alias /cms/ "/usr/local/staff/code/cms/"
    Alias /index.php "/usr/local/staff/code/index.php"

    Options +FollowSymlinks -Indexes

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$    [NC]
    RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com)$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/clients/%1%{REQUEST_URI}/ -d
    RewriteRule ^/.*[^/]$ http://%{HTTP_HOST}$0/index.php [R=301,L]

    RewriteCond %{HTTP_HOST} !^(www\.)?sites\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(.+)\.sites\.example\.com$ [NC]
    RewriteRule ^/.*$ /clients/%1$0 [QSA,E=TEST:%1,PT,L]
</VirtualHost>
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Fri Jun 19, 2009 10:13 am

Code: Select all
script '/data/sites/sites.example.com/htdocs/clients/<something>/index.php' not found or unable to stat

Is that supposed to match
Code: Select all
Alias /index.php "/usr/local/staff/code/index.php"
? It won't because the internal URL/path is going to be /clients/<something>/index.php. Try
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/.*)?$  "/usr/local/staff/code/cms/"
AliasMatch ^/clients/[^/]+/index\.php$ "/usr/local/staff/code/index.php"
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Sat Jun 20, 2009 8:26 am

I replaced the Alias directives with the AliasMatch directives that you suggested.

When I go to test.sites.example.com without specifying index.php I get a 403 Forbidden message, with this in the log:
Code: Select all
Directory index forbidden by Options directive: /data/sites/sites.example.com/htdocs/clients/test/


When I specify test.sites.example.com/index.php this seems to work. The page loads (i haven't tested it extensively but seems OK).

When I specify test.sites.example.com/cms/ I again get the 403 Forbidden message with the log message:
Code: Select all
Directory index forbidden by Options directive: /usr/local/staff/code/cms/


When I specify test.sites.example.com/cms/index.php the URL is rewritten to test.sites.example.com/clients/test/cms/index.php and I get a 404 Not Found. along with this log message:
Code: Select all
File does not exist: /data/sites/sites.example.com/htdocs/clients/test/clients




richardk wrote:
Code: Select all
script '/data/sites/sites.example.com/htdocs/clients/<something>/index.php' not found or unable to stat

Is that supposed to match
Code: Select all
Alias /index.php "/usr/local/staff/code/index.php"
? It won't because the internal URL/path is going to be /clients/<something>/index.php. Try
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/.*)?$  "/usr/local/staff/code/cms/"
AliasMatch ^/clients/[^/]+/index\.php$ "/usr/local/staff/code/index.php"
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Sat Jun 20, 2009 8:58 am

Add
Code: Select all
DirectoryIndex index.php


Replace
Code: Select all
^/clients/[^/]+/index\.php$

with
Code: Select all
^/clients/[^/]+/(index\.php)?$


Edit: Also replace
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/.*)?$  "/usr/local/staff/code/cms/"

with
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/(.*))?$  "/usr/local/staff/code/cms/$2"
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nkj » Thu Jun 25, 2009 5:13 am

Thanks! things seem to be working now except for one last thing. It still does not default to assuming the index.php when using the /cms/ URL

http://test.sites.example.com/cms/ gives me a 403 Forbidden to access "/clients/test/cms/"

however,
http://test.sites.example.com/cms/index.php works.


richardk wrote:Add
Code: Select all
DirectoryIndex index.php


Replace
Code: Select all
^/clients/[^/]+/index\.php$

with
Code: Select all
^/clients/[^/]+/(index\.php)?$


Edit: Also replace
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/.*)?$  "/usr/local/staff/code/cms/"

with
Code: Select all
AliasMatch ^/clients/[^/]+/cms(/(.*))?$  "/usr/local/staff/code/cms/$2"
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

PreviousNext

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 1 guest

cron