How can I config apache mod rewrite? - apache

My web root is /var/www/test/public, eg:http://www.test.local is rewrite to here,
but there's a folder /var/www/test/documents/ where I store some pictures to let others download. How can I config my apache rewrite mod to allow users to download pictures in this documents folder by a url?
Can anyone help me? Thanks!

You don't need mod_rewrite for that. You can use an Alias directive to map that images directory into your document root:
Alias /images /var/www/test/documents
^--web path ^---file system path
That'd make any request for example.com/images be internally redirected to /var/www/test/documents, even though that documents dir is not within your site's webroot.

Related

Absolute path to subfolder

I have apache setup to serve sites in /var/www, and I have many sites in this directory. Now I don't own any of these sites and cannot update any of their content.
The problem is most of these websites use absolute path for their assets like <img src="/img/foo.png">, and apache tries to load them from /var/www/ instead of say /var/www/example1/.
I tried to add a .htaccess to /var/www/ to rewrite every paths to assets to a subdirectory, but I can't find a way to make assets from /var/www/example1/ loaded from this directory and assets from var/www/example2/ loaded from example2 instead of example1.
Is there a way to make this work? I don't really like the .htaccess solution in /var/www/, at first I wanted to use a vHost configuration for every sites but I didn't find how to do it (at least without having a domain name for each site).
Thanks!

Can't find apache config redirect in config files

the goal
Redirecting mydomain.com/something to mysite.com/something
the problem
Any domain that you throw at the server gets 302 redirected to www.mysite.com and, for the life of me, I can't find the config rule that is doing this!
what i've done so far
I've grepped everything in the /etc/apache2 folder for 'mysite.com', 'Redirect', 'redirect', and other, as well as scour by hand all the apache config files looking for this redirect rule to no avail.
Any and all help in finding the culprit redirect is greatly appreciated. I'm not a sysadmin so there must be a place I don't know of where this might be other than the /etc/apache2 folder...
Thank you kindly for helping me with my first SO question :)
The file which includes redirection rules does not have to be in /etc/apache2 folder. Check your document root folder (the path of which you can find in your vhost configuration file) for .htaccess files. They may have redirect rules too.
The problem was with an .htaccess in the docroot of another domain on the server. This other domain was higher alphabetically and was treated as the default domain.
I solved the problem by creating a new vhost for the domain that i wanted to redirect and it was handled properly.
Thank you all for your help!

Apache symlink to a folder outside of web root

Here is the folder layout.
/outside is not normally accessible anywhere on example.com
/public_html is the folder for http://example.com
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
Thanks!
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
You can't. As far as I have found out, Apache prevents directives in .htaccess files linking to locations outside the current web root.
You would have to do this in the central configuration:
Alias /outside /path/to/your/outside
You may be luckier with a symlink if you can turn FollowSymLinks on.

Does the recursive .htaccess rule affects subdomains pointing to subdirectories?

Suppose I have a site example.com which points to the root directory of my hosting account and it has a .htaccess file in that directory. Say I have an other domain stuff.example.com which points to the same hosting account, but to a subdirectory of it, not to the root directory, and that subdirectory also has a .htaccess file in it.
If I visit stuff.example.com then will its .htaccess file be affected by the .htaccess settings of the root directory? Or htaccess search starts from the directory where the domain points to, so in this case the htaccess file in the parent directory is not taken into account?
Surprisingly the Apache docs don't ever explicitly answer this, as far as I can tell. But the htaccess tutorial gives the example that if a file is requested from /www/htdocs/example, then Apache looks for the following .htaccess files:
/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess
which presumably leads outside of the DocumentRoot. So it seems that the answer to your question is yes, Apache will read all .htaccess files all the way up to /.
will its .htaccess file be affected by the .htaccess settings of the root directory?
Yes. Where your web root is doesn't matter.

.htaccess Redirect request to files exts in particular folder only

How do you write rules to redirect all requests to *.php and *.html files in upload/ folder to a text file name forbidden.txt in root www folder. What I'm trying to do exactly is preventing script execution in this dir by redirecting those requests to the text file
Note: The upload/ folder is accessibly by ftp used by a group of people to upload files so I cannot place htaccess inside this folder.
Create an .htaccess file at the root level of your site containing
RedirectMatch ^/upload/.+(html|php)$ http://www.yoursite.com/forbidden.txt
You could also try switching off the PHP engine in that directory by creating an .htaccess file in /upload/ containing:
php_value engine off
although you would need to ensure that people cannot upload files with the name .htaccess
Put your htaccess rules in httpd.conf instead.
If you can't edit httpd.conf, then your best bet is to not allow web access to that directory at all. Let FTP users access a folder outside of your web directory and then provide a mechanism for retrieving the file contents.
You could name that directory "upload". Then you could have your .htaccess file make requests to /upload/myfile execute upload.php, which finds ../upload/myfile and spits backs its contents. This way it would appear to users that they are accessing the "upload" folder directly, but you would the level of control you want through the PHP script.