Forum Moderators: phranque

Message Too Old, No Replies

Sharing same code files across multiple domains.

         

notmessenger

5:40 am on May 29, 2005 (gmt 0)

10+ Year Member



I have read through a large number of posts on this site and have been able to solve part of my problem, but I am still experiencing some difficulties that I hope someone can assist me with.

Let me try and explain what I am trying to accomplish.

I have a web application that I am hosting at www.example.com/engine. Visiting www.example.com displays information about the application, how to sign up for it, etc. The actual application resides at www.example.com/engine and is used to run everyone's co-branded version of it.

A client (or their customers) can access their own co-branded version of the engine by either accessing a url that is pointed to the same server as the engine, such as www.123.com or www.abc.com, or by linking to a subdomain from their own site, such as 123.example.com.

At the moment, I am correctly loading the engine when a user enters the url of www.123.com by using the code below:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*(123)\.com [NC]
RewriteRule .* /home/www/example.com/engine/index.php?domain=%1 [L]

The condition has been written so that either www.123.com or 123.com triggers the RewriteRule. The RewriteRule loads a PHP document that is passed the URL entered so that the engine can properly co-brand the application. So while the user is fed a script that is actually the engine code from a central directory, the URL remains as entered (ie www.123.com). Based on this URL, any links on the page displayed begin with [123.com...] which is what is expected. Let's say one such link is [123.com...] If I click on this link, I am brought right back to the same page. Basically, it appears as if the Rewrite Conditions and Rules will not allow for all files to be re-written from the centralized directory.

Is it possible to have several domains, such as www.123.com or www.abc.com, to load the same centralized code? How can I modify my existing rewrite rules to allow all links to be rewritten so every request is routed to the centralized code?

Thanks!

dcrombie

11:48 am on May 29, 2005 (gmt 0)



Try doing it in Apache or .htaccess using:

php_value include_path .:/path/to/common/PHPCodeDir

Then you can directly "include" your files without specifying a path.

;)

notmessenger

4:56 pm on May 29, 2005 (gmt 0)

10+ Year Member



I am unsure where to place this code. Would this get placed underneath the RewriteCond line?

jdMorgan

5:31 pm on May 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



notmessenger,

Welcome to WebmasterWorld!

Here's a tweaked version to accept only "www" or non-www domains.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b](123)\.com [NC]
RewriteRule .* /home/www/example.com/engine/index.php?domai[b]n=%2 [R=302[/b],L]

However, if you want to redirect *any* file request for that domain over to "engine" using that requested file name, then you're going to need to capture the requested file name and insert that into the substitution URL. As it is, any request for any file in the 123.com domain will be redirected to "engine/index/php" -- there is no provision to use the originally-requested filename.

If you wanted to pass the requested file as a parameter, you could use something like:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(123)\.com [NC]
RewriteRule (.*) /home/www/example.com/engine/index.php?domain=%2&page=$1 [R=302,L]

If you wanted to actually use the requested filename on the other server, then it would be more like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(123)\.com [NC]
RewriteRule (.*) /home/www/example.com/engine/$1?domain=%2 [R=302,L]

Neither of these is likely to be exactly what you want, but without more info, I can't suggest anything more specific.

Jim

notmessenger

6:53 pm on May 29, 2005 (gmt 0)

10+ Year Member



Jim

Actually, your last example was almost exactly what I needed/wanted. The only thing I had to change was to remove the R=302, so now my configuration looks like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(123)\.com [NC]
RewriteRule (.*) /home/www/example.com/engine/$1?domain=%2 [L]

Do you anticipate this causing any problems? I had to remove it because it was forcing the browser to redirect and it would get stuck in a loop of sorts. Since all of the files/code reside on the same server I figured I don't need to do a redirect and once I removed it everything is working fine.

I am now passing the domain information to each and every script instead of just to index.php but once I started thinking about it, I realized that this is what I wanted anyways and just didn't realize it. If a user enters www.123.com/somepage.php, I need to be able to load that page and pass it the domain as well - I was erroneously thinking of only one path into the site since that is what I was focused on in my testing. Thank you for taking care of that problem for me to! :-)

For anyone who may use this thread in the future, I have taken the liberty of now combining my conditions together so that I can save space and sanity.

Example is below:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(123)\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?(abc)\.com [NC]
RewriteRule (.*) /home/www/example.com/engine/$1?domain=%2 [L]

A parting question for anyone who may be able to assist me. I have now begun trying to use RewriteCond to limit www.example.com/engine from being directly accessed but have only succeeded in limiting everything - even the correctly working before conditions. I will play some more and post what I have if it's not working or does start working.

Thank you very much!

jdMorgan

7:40 pm on May 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you anticipate this causing any problems? I had to remove it because it was forcing the browser to redirect and it would get stuck in a loop of sorts. Since all of the files/code reside on the same server I figured I don't need to do a redirect and once I removed it everything is working fine.

No. Actually, I misread your code, and that was my mistake.

Rather than add another condition, you can simply use an alternate subpattern in the RewriteCond that you had; Alternate strings separated by a solid pipe character are equivalent to an [OR] in this case. Note that posting on this board changes the "¦" to a broken pipe, and you'll have to correct that before use. Just re-type it using the pipe key on your keyboard -- usually the Shift-\ key.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(123¦abc)\.com [NC]
RewriteRule (.*) /home/www/example.com/engine/$1?domain=%2 [L]

To prevent direct access, you have to check the actual client request header, which looks something like this:

GET /engine/somefile.ext?optional_query_string HTTP/1.1

So, as a separate code section, you could add:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /engine/
RewriteRule ^/engine/ - [F]

That RewriteCond will accept GET, POST, PROPFIND, and all other HTTP methods, which are all uppercase and vary in length between 3 and 9 characters, followed by a space, and then "/engine/" followed by anything else. The trick here is that while the local URL-path "seen" by RewriteRule will be updated by other rewrites, THE_REQUEST always contains the originally-requested URL-path. As such, this rule blocks direct client requests for anything in the /engine directory. As written, it will generate a 403-forbidden response, although you could also change it to do a rewrite or redirect to some other URL-path if desired.

If this is for use in .htaccess, then delete the leading slash from "/engine/" in the RewriteRule pattern only.

Jim