.htaccess masked forwarding for certain folder/directory within domain - apache

Let's say I have a domain called www.customer1.com and www.customer2.com. I want to run all the pages of these sites separately... but items found within certain paths, I'd like to reference from one domain to another in a masked forwarded manner for SEO purposes and to avoid having to place files in two different FTP accounts.
The target folders are
/images
/pdfs
FOr example if a call is made to
www.customer2.com/images/[any image] then I want the masked forwarder to kick in to serve a file that is located at www.customer1.com/images/[filename requested]. Same goes for anything found after /pdf/ in the same example.
However all other pages should remain referencing to internal files within.
I have limited understanding of .htaccess and frankly lost as to how to approach anything beyond a very simple 30

Not sure you understand the concept here, as the "forwarding" would equate to an external redirect, there is no "masking" anywhere. The closest thing there is is reverse proxying:
RewriteEngine On
RewriteRule ^(images|pdf)/(.*)$ http://www.customer1.com/$1/$2 [L,P]
You need mod_proxy to do this and those rules need to be in the htaccess file in your customer2.com's document root.
You can also do this in customer2.com's server/vhost config:
ProxyPass /images/ http://www.customer1.com/images/
ProxyPass /pdf/ http://www.customer1.com/pdf/

Related

Need to configure .htaccess, so multiple folders will act as if they are their own separate root folders - for the code running on them

For example:
mydomain.com/site1
mydomain.com/site2
I need to install an application on /site1 that will think that it is on the root folder. (In this case PHP, js, CodeIgniter, but could be anything)
So for example, links/references for files such as "/file.jpg" (in code that is in the site1 folder, such as at mydomain.com/site1/code.js) will really load from mydomain.com/site1/file.jpg
And also the code would not be able to see any folder below site1, so that is basically the root folder. And similar thing would be at site2, so the 2 are separate root folders.
I thought this would be some kind of simple .htaccess file installed at mydomain.com/site1 with a redirect, or some kind of a reverse proxy, but so far everything I tried did not work.
I can't seem to find even any such example even on stack overflow..
Any ideas?
The easiest way to do this would be to create an additional VirtualHost, for internal use, called internal1, whose RootDirectory is, you guessed it, /var/www/mydomain.com/htdocs/site1 where the main site is in /var/www/mydomain.com/htdocs.
Then in mydomain.com you reverse proxy /site1 to internal1 (you'll have to put it into /etc/hosts and alias for localhost). The second request will have its DOCUMENT_ROOT point to site1, as requested (and its ServerName changed to internal1):
ProxyPass /site1/ http://internal1/
ProxyPassReverse /site1/ http://internal1/
(Not sure about the trailing slashes)
Now, accessing yourdomain.com/site1/joe.html will trigger a second internal connection to internal1/joe.html, which will contain, say, 'src="/joe.jpg"'; and here's where ProxyPassReverse will come into play, rewriting this in 'src="yourdomain.com/site1/joe.jpg"' so that everything will work.
errata corrige
The above is not correct, thanks #MrWhite for pointing this out. ProxyPassReverse is not enough as it only rewrites headers. From the Apache documentation (emphasis mine):
Only the HTTP response headers specifically mentioned above will be
rewritten. Apache httpd will not rewrite other response headers, nor
will it by default rewrite URL references inside HTML pages. This
means that if the proxied content contains absolute URL references,
they will bypass the proxy. To rewrite HTML content to match the
proxy, you must load and enable mod_proxy_html.
(The method is dirty as all Hell: every HTTP call incurs one extra connection and two rewrites, one going in, a larger one going out).
Of course, if the link is built using e.g. Javascript, it might well be that the proxy code will not recognize it as a link, will leave it unchanged, maybe with the "internal1" name inside somewhere, and the app will break.
However, #arkascha has the right of it - you should cure the cause, not the symptom. You can maybe rewrite the environment of the apps so that they run without troubles even if they are in a subdirectory. Or you could try injecting <base href="https://example.com/site1"> in the output HTML.

Howto use path/directory outside the domain root via symlink without access to domain config?

I am working an a shared hosting plattform which does not allow to edit or access the Apache config file.
The goal is to access the same files from two different domains which point to different domain roots:
test.example.com ---> /test_root/web
public.example.com ---> /public_root/web
Now I would like to access the same files using test.example.com/some/files/... and public.example.com/some/files/...
Of course I could simply copy the files to /test_root/web/some/files/... and to /public_root/web/some/files/... but this obviously just an example. In reality the files are a helpdesk system which should be integreated both into the test- and public-site. Copying the files would include maintaining two different systems, etc.
The goal is, to place the the files somewhere outside the two domain roots and make them available from both domains:
/test_root/web/some/files ---> /path/to/some/files
/public_root/web/some/files ---> /path/to/some/files
I created symlink to achive this (ln -s ...) but this does not work out. When I access on of the domains (e.g. test.example.com/some/files) I only get a blank page without any information what whent wrong.
I assume that the Apache is not configured to follow the symlinks. Without access to the Apache config I can neither check nor fix it.
Adding Symlinks to .../web/.htacess does not make any difference:
// test_root/web/.htaccess
Options +FollowSymLinks
So the question is: How can I make files outside the domain root available within a domain? Is this even possible? Is this possible using symlinks?
Add this in you .htaccess file.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} test.example.com/path/to/files [NC]
RewriteRule ^(.*)$ http://public.example.com/path/to/files [R=301,NC]
Test out this code. /path/to/files must be under /public_root/web/.
Something like /public_root/web/path/to/files/

Access root domain from subdomain on hosted apache server

I had a domain:
mydomain.com
pointing to a hosted apache server 'premium' account that can host multiple domains.
I bought another domain: anotherDomain.com which I set up as an 'add on' domain with my web host. I can access the anotherDomain in several different ways:
mydomain.com/anotherDomain.com
anotherDomain.mydomain.com
and
anotherDomain.com
However, only when using the first method can I access 'generic' files on mydomain.com from anotherDomain.com (using relative addressing).
I was told there is a script I can write so anotherDomain.com can access 'root' files at mydomain.com, using relative addressing, but they cannot tell me how to do it. I've looked around the net, but although there are lots of similar sounding questions, I cannot find how to do it.
Just to restate the problem: I want to be able to access files in mydomain.com, just like I can when anotherDomain.com is accessed like: mydomain.com/anotherDomain.com, when it is accessed like: anotherDomain.mydomain.com or anotherDomain.com
Example:
If I access anotherDomain.com using the URL mydomain.com/anotherDomain.com then, in the index.html for anotherDomain.com I can have:
<img src='../imgs/generic.jpg'/>
Which access the 'generic' image in the imgs folder for mydomain.com. Unfortunately, when I access this page using the URLs: anotherDomain.mydomain.com or anotherDomain.com, this doesn't work.
First of all, I assume you don't have access to your server's config files and thus have to deal with the restricted possibilities of .htaccess. If you have access there would be much better ways to handle this.
I would suggest to circumvent the problem. Have a look at the other answers and the comments. You went for a highly complex setup. You might get that working, but it will be a pain in the ass in the long run.
I will elaborate a bit and explain a few things in my answer.
As I understand you have the following file structure on your server:
public_html/
|
+-- index.html // the index of mydomain.com
|
+-- imgs/
| |
| +-- generic.jpg
|
+-- anotherDomain.com/
|
+-- index.html // index of anotherDomain.com
Suppose you browse http://anotherDomain.com/.
When the browser tries to load generic.jpg it will create this URL: http://anotherDomain.com/../imgs/generic.jpg. This will, however, by almost every browser, be rewritten as http://anotherDomain.com/imgs/generic.jpg.
So you have to tell the server how to server this file.
You can create a rule as #anubhava suggests. If you access http://anotherDomain.com/imgs/* redirect it to the imgs dir on the virtual host http://mydomain.com/. This way the content will appear to belong to mydomain.com.
I would suggest creating a symlink instead, if you have the possibility.
|
+-- anotherDomain.com/
|
+-- index.html // index of anotherDomain.com
|
+-- imgs --> ../imgs/
This way you can access all the images easily. However, they will appear as content of anotherDomain.com. This can be seen as advantages or as already mentioned as disadvantage (search engines.) Creating a symlink can also sometimes be done by using PHP (symlink function) if your provider does not support it via its interface.
After wasting time trying to do this with .htaccess files, I finally worked out a way to do it in plain old html. In the header, before all other links, put:
<base href="http://mydomain.com/anotherDomain.com" />
And thats it. Remove this for development on your local machine. ;-)
Remember to be a bit careful when using the base tag.
Edit:
I've found this causes other problems. For example, relative links go to http://mydomain.com/anotherDomain.com, rather than http://anotherDomain.com.
Adding .htaccess code to the http://mydomain.com root directory like:
RedirectMatch /anotherDomain.com(/)?$ http://anotherDomain.com
Solves this, but introduces other problems. Still looking for a good answer to this question... Anybody?
Try this code in your public_html/anotherDomain.com/.htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# for images only
RewriteCond %{HTTP_HOST} ^(www\.)?anotherDomain\.com$ [NC]
RewriteRule ^(imgs/.+)$ http://mydomain.com/anotherDomain.com/$1 [L,NC,R]
# or for all files
RewriteCond %{HTTP_HOST} ^(www\.)?anotherDomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/anotherDomain.com/$1 [L,NC,R]
Remove (or comment out) your <base... tag.
Accessing the same content via several different domains is maybe not a great idea. This can make your content appear like duplicated contents from search engines. If you keep that idea you should at least try to register all your contents with a canonical url metatag or attribute.
Now I usually like relative urls, as always preficing url with the domain makes them harder to reuse via proxys (but when using ajax stuff, for example, relative url are not enough, you'll always end with some absolute url in some places). But here you use relative url with '../imgs/generic.jpg'. It means this url refers more to the way you've been putting the files on your server than to something which is meaningful. It could be meaningfull on a single domain, but to share stuff between domains it's not. What if some day you need to move the new domain files and directory root on another place (on another server?). From the user (and bots and search engines) your domains are not related. Any proxy that you do not own would request the asset several time if it is asked via subdomain.mydomain.com/foo.jpg and subdomain.com/foo.jpg, so trying to share this stuff from your domains is not meaningful for the rest of the web, it's just for your own managment, on your side. So feel free to make it simple to manage.
You'd better manage your url in a way where the url means something to you, like '/common/imgs/generic.jpg' and '/site/imgs/custom.jpg'. Then on the domain Virtualhosts, server-side, you can work on the mapping url->directory & files. This mapping can be set via Alias, AliasMatch, and of course mod_rewrite directives.
For example a simple
Alias /common /path/to/mydomain/assets
Would allow you to map all the mydomain.com assets on the Virtualhost containing this instruction. The day you will decde to move all the things on your webserver you'll simply have to rework the apache rewrites and alias, and not all you code url management.

Generic .htaccess for multiple websites stored in subdirectories

My development environment is set up for using a single host (localhost). I am developing multiple websites on my machine, each stored under its own directory like this:
/var/www/site1
/var/www/site2
...
The document root is set to /var/www on my machine.
I am using URL rewriting for most of these websites and most of the .htaccess files will rewrite a sub-directory to GET parameters in different ways like this:
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
I also tend to copy some of these websites under different directories and, when I do that, I have to make a lot of changes in the .htaccess files for the website that I'm copying.
I would like to know if there is a way to define a constant that contains the website's root directory (not the host's document root) and how can that be used with the rewrite rule so that I would need to change only one line of code (setting this constant to a different value) when copying a website.
Putting this in a different form, is there a way to perform rewrites that relate to a website root instead of a host / %{HTTP_HOST} (i.e. the "host" for the website being localhost/site1 instead of localhost) and how can this be done?
I have tried removing the host from each request at the beginning of the script and prepending it back at the end of the script, but this does not work with rewrite rules that use the [L] option.
Thank you!
Regards,
Lucian
You could make an htaccess file with rules like this:
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
And put this in the directory /var/www/site1, and if you want for it to apply to site2, change the RewriteBase and put the rules in /var/www/site2.

Where is the proper place for mod_rewrite entries?

For the love of God, I can't seem to get this mod_rewrite working properly. Instead of doing brute force trial-and-error, let me ask here.
I want mod_rewrite rules to apply to ALL domains.
I want mod_rewrite entries in httpd.conf
I want to get rid of this WWW virus (for SEO purposes):
http://www.example.com > http://example.com
I want to get rid of index.html (for SEO, google indexes it instead of just domain):
http://www.example.com/index.html > http://example.com
http://www.example.com/some/index.html > http://example.com/some/index.html
Domains are inside <virtualhost> entries. I couldnt figure out where to put what or which one should take priority. As i mentioned, I would like to apply these 2 rules to ALL DOMAINS in the server.
The situation is exacerbated by ssl.conf. Will all these need to be entered into ssl.conf too? What will happen when there are 2 redirects like:
http://www.example.com/index.html > http://example.com/index.html > http://example.com
Thank you so much. This has quickly become all so confusing.
Maria
This solves it for me. As I suspected, there is a whole lotta difference where rewriterule is applied. Many people including mean seems to be unaware of this.
http://wiki.apache.org/httpd/RewriteContext
The Apache HTTPD Server deals with requests in discrete phases. While this is usually transparent to the user and administrator it does have an effect on the behaviour of mod_rewrite when rulesets are placed in different contexts. To oversimplify a little, when rules are placed in VirtualHost blocks (or in the main server context) they get evaluated before the server has yet mapped the requested URI to a filesystem path. Conversely, when rules are placed in .htaccess files, or in Directory blocks in the main server config, they are evaluated after this phase has occured.