Forum Moderators: phranque
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!
php_value include_path .:/path/to/common/PHPCodeDir
Then you can directly "include" your files without specifying a path.
;)
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]
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]
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
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!
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]
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]
If this is for use in .htaccess, then delete the leading slash from "/engine/" in the RewriteRule pattern only.
Jim