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.
Related
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/
basically i have a web server set up where all sites are in /var/www but i want to be able to do development in ~/public_html/sitename so i set userdir's up in apache but the links still goto the /var/www directories pages.
I know i could go in and change all the links, but i think an htaccess would be easier.
So my question is, is it possible to say:
We have a folder in my public_html on home which is accessed via example.com/~jackyyll/project
I want all requests from that project to be redirected to ~/jackyyll/project/request_here
I've tried some things in the .htaccess but nothing is working.
Thanks
If you don't want to use mod_userdir, a simple AliasMatch can do the trick. Here is what I use to map www.kylheku.com/~kaz/ to /home/kaz/public-www/, et cetera, straight from my httpd.conf:
# /~user/blah goes to /home/user/public-www/blah
AliasMatch ^/~([a-z]+)/(.*) /home/$1/public-www/$2
# /~user and goes to /home/user/public-www/
AliasMatch ^/~([a-z]+)$ /home/$1/public-www/
AliasMatch has some capabilities resembling mod_rewrite, except, just like Alias what it does is it takes URL's coming into the server and maps them to paths. The output is not re-injected into the request processing chain. Any URL mapped by Alias or AliasMatch bypasses the VirtualHost-s and their docroots. It's like a special exception mechanism.
The .htaccess in the user's public-www directory works just fine. I have RewriteRules there and other cruft.
Of course, this is a lot simpler than mod_userdir. It maps all possible usernames to all possible userdir directories, whether or not they exist. mod_userdir has more bells and whistles, like restricting which users have a userdir, and alternative userdirs certain users.
Trying to configure apache2 to load example.com/forum/ from a different document root, relative to the site root. Forums are installed somewhere else on the server.
Is there a directory alias command? I've found the alias configuration entry for apache, but had no luck.
Basically, I want example.com to have the same directory its always had, but example.com/forum/ to be hosted somewhere else, on the same server.
I tagged this question with mod_rewrite because I thought maybe it would be the key, here.
Cheers!
Alias is the right way, unless you have some subtlety that you didn't reveal in your question.
# http.conf
Alias /forum /usr/lib/bbs/ # or whatever
The job of Alias is to take the abstract URL coming into your system and map it to a concrete filesystem path. Once it has done that, the request is no longer an URL but a path. If there is no Alias or similar directive handling that URL, then it will get mapped to a conrete path via DocumentRoot.
If this isn't working, you have to debug it further. Are you getting errors when you access /forum? Look in the error log.
It all depends of what you want. You can "hardlink" with real path and it works (so you were right to think it could work with mod_rewrite).
Quick sample (that works on my production domains) to make an internal change (I add a subdirectory):
RewriteRule (.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
So you can easily do something like:
RewriteRule ^/forum/(.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
And my suggestion would be that if you plan to have more rewrite rules, keep everything homogeneous, i.e.: keep on using only rewrite rules, so use my suggestion above. This way you'll not get a bad mix of Alias, RewriteRules and so on. For nice and clean stuff: keep everything homogeneous.
I want to map a number of directories in a URL:
www.example.com/manual
www.example.com/login
to directories outside the web root.
My web root is
/www/htdocs/customername/site
the manual I want to redirect to is in
/www/customer/some_other_dir/manual
In mod_alias, this would be equal to
Alias /manual /www/customer/some_other_dir/manual
but as I have access only to .htaccess, I can't use Alias, so I have to use mod_rewrite.
What I have got right now after this question is the following:
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L]
this works in the sense that requests are recognized and redirected properly, but I get a 404 that looks like this (note the absolute path):
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
However, I have checked with PHP: echo file_exists(...) and that file definitely exists.
why would this be? According to the mod_rewrite docs, this is possible, even in a .htaccess file. I understand that when doing mod_rewrite in .htaccess, there will be an automated prefix, but not to absolute paths, will it?
It shouldn't be a rights problem either: It's not in the web root, but within the FTP tree to which only one user, the main FTP account, has access.
I can change the web root in the control panel anytime, but I want this to work the way I described.
This is shared hosting, so I have no access to the error logs.
I just checked, this is not a wrongful 301 redirection, just an internal rewrite.
In .htaccess, you cannot rewrite to files outside the wwwroot.
You need to have a symbolic link within the webroot that points to the location of the manual.
Then in your .htaccess you need the line:
Options +SymLinksIfOwnerMatch
or maybe a little more blindly
Options +FollowSymlinks
Then you can
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/site/manual/$2 [L]
where manual under site is a link to /www/customer/some_other_dir/manual
You create the symlink on the command line with:
ln -s /www/htdocs/customername/site/manual /www/customer/some_other_dir/manual
But I imagine you're on shared hosting without shell access, so look into creating symbolic links within CPanel,Webmin, or whatever your admin interface is. There are php/cgi scripts that do it as well. Of course, you're still limited to the permissions that the host has given you. If they don't allow you to follow symlinks as a policy, you cannot override that within your .htaccess.
AFAIK mod_rewrite works at the 'protocol' level (meaning on the wire HTTP). So I suspect you are getting HTTP 302 with your directory path in the location.
So I'm afraid you might be stuck unless.. your hosting lets you follow symbolic links; so you can link to that location (assuming you have shell access or this is possible using FTP or your control panel) under your current document root.
Edit: It actually mentions URL-file phase hook in the docs so now I suspect the directory directives aren't allowing enough permissions.
This tells you what you need to know.
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
It interprets RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L] to mean rewrite example.com/manual/ as if it were example.com/www/htdocs/customername/manual/.
Try
RewriteRule ^manual(/(.*))?$ /customername/manual/$2 [L]
instead.
I'm starting up a new web-site, and I'm having difficulties enforcing my desired file/folder organization:
For argument's sake, let's say that my website will be hosted at:
http://mywebsite.com/
I'd like (have set up) Apache's Virtual Host to map http://mywebsite.com/ to the /fileserver/mywebsite_com/www folder.
The problem arises when I've decided that I'd like to put a few files (favicon.ico and robots.txt) into a folder that is ABOVE the /www that Apache is mounting the http://mywebsite.com/ into
robots.txt+favicon.ico go into => /fileserver/files/mywebsite_com/stuff
So, when people go to http://mywebsite.com/robots.txt, Apache would be serving them the file from /fileserver/mywebsite_com/stuff/robots.txt
I've tried to setup a redirection via mod_rewrite, but alas:
RewriteRule ^(robots\.txt|favicon\.ico)$ ../stuff/$1 [L]
did me no good, because basically I was telling apache to serve something that is above it's mounted root.
Is it somehow possible to achieve the desired functionality by setting up Apache's (2.2.9) Virtual Hosts differently, or defining a RewriteMap of some kind that would rewrite the URLs in question not into other URLs, but into system file paths instead?
If not, what would be the preffered course of action for the desired organization (if any)?
I know that I can access the before mentioned files via PHP and then stream them - say with readfile(..), but I'd like to have Apache do as much work as necessary - it's bound to be faster than doing I/O through PHP.
Thanks a lot, this has deprived me of hours of constructive work already. Not to mention poor Apache getting restarted every few minutes. Think of the poor Apache :)
It seems you are set to using a RewriteRule. However, I suggest you use an Alias:
Alias /robots.txt /fileserver/files/mywebsite_com/stuff/robots.txt
Additionally, you will have to tell Apache about the restrictions on that file. If you have more than one file treated this way, do it for the complete directory:
<Directory /fileserver/files/mywebsite_com/stuff>
Order allow,deny
Allow from all
</Directory>
Can you use symlinks?
ln -s /fileserver/files/mywebsite_com/stuff/robots.txt /fileserver/files/mywebsite_com/stuff/favicon.ico /fileserver/mywebsite_com/www/
(ln is like cp, but creates symlinks instead of copies with -s.)