.htaccess for apache userdir's - apache

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.

Related

.htaccess masked forwarding for certain folder/directory within domain

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/

Is it necessary to set [DirectoryIndex] while not using index.php?

My sites root access is managed by htaccess: it redirects various aliases to their own home files /en/home for english /de/home for Deutsch etcettera. Previously, I used index.php to route and redirect all that, and hence the DirectoryIndex had something like this:
DirectoryIndex /index.php
Now, however, there is no index.php file, so I commented it
# DirectoryIndex /index.php
Would it be better to uncomment is and set it to the default /en/home (with or without .php because in this case? I have set up rules sohat my pages in browser also work when no extension is given)
DirectoryIndex /en/home
In all the above cases, my websites work fine and I don't see ANY change when I set either of the three instances as above. but ... "there's gotta be one best ain't it?"
Thanks!
If you have the rules written in .htaccess it is best not to repeat the rules in whatever php config and routing functions you are using. Routing through apache (your .htaccess) is much faster than subverting routes through php, though you will not realize the gains without a pretty high volume of traffic.

Apache virtual directory without redirect

What I'm trying to accomplish is pulling content from a directory that is not the same as the url path. For example:
URL: example.com/
path: /www/production/
Currently, the root url pulls content from the path above. What I want to do is something like this:
URL: example.com/
path: /www/production/root/
So the base URL "example.com" should pull data not from /www/production/ but from /www/production/root/.
I think this is called a Virtual Directory in IIS. Is there something like this in Apache?
Edit for clarification:
I have a ton of existing content that I do not want to have to restructure yet. However, the root site is being completely rebuilt and is going to be quite a bit bigger. What I want is each microsite to have it's own directory, such that requests for example.com/ should pull content from /www/production/root/ while requests for example.com/microsite/ should still pull content from /www/production/microsite/.
Hopefully that makes more sense. :)
Its not exactly clear what your problem is. If you just want all URLs to be taken relative to some location on the server, then you can use the DocumentRoot directive:
DocumentRoot /www/production/root/
If you want to only have certain urls go to the new place, then you can either use the Alias or AliasMatch directives from the mod_alias module. These can map either prefixes on a URL, or url Regexes to other server locations. If even this isn't sufficient, you can use mod_rewrite which allows for arbitrary chains of url rewriting, but can get very hairy to maintain, so you're better off avoiding that module if you can.
http://httpd.apache.org/docs/2.0/mod/mod_alias.html
If I understand your question correctly, then the following, in your VirtualHost config, does what you're asking for:
ServerName example.com
DocumentRoot /www/production/root/

mod_rewrite to absolute path in .htaccess - turning up 404

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.

How can I redirect requests to specific files above the site root?

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.)