Creating multiple files for different rewrite rules all using the same http conf file? - apache

Im quite new to rewrite rules so there may be a very simple way of doing this but i cant seem to find much information on it.
Anyway i have a rewriterules file containing about a thousand rules for many different sites, so is there any way to create multiple files just for easier handling of the large amount of rules ?
For example i have three sites:
A.com
B.com
C.com
I want a seperate file for each sites redirects, but also want one universal file for rules that are applicable to all of them ?
Is there anyway to do this ?

If you can use the symbolic links, .htaccess file including all RewriteRules is located to setting directory [Setting Dir].
Therefore in site A document root, you set the symbolic link as
$ cd [Document Root A]
$ ln -s [SettingDir]/.htaccess .htaccess
in the same way, in site B, you set
$ cd [Document Root B]
$ ln -s [SettingDir]/.htaccess .htaccess
...

Related

What is the purpose of the wget startup file option "add-hostdir"?

The wget 1 man pages describe the option "add-hostdir" as follows:
Enable/disable host-prefixed file names. ‘-nH’ disables it.
Unfortunately, I am too ignorant to understand this.
Since "-nH" disables it, it must have something to do with spanning hosts, but it isn't described there either.
Can someone explain what it does?
I am using wget 1.19.1
Consider simple example
wget -r http://www.example.com
create directory www.example.com and place index.html inside it
wget -r -nH http://www.example.com
just places index.html in current working directory, that is you have one level less (top one) of catalog hierarchy when using -nH.
Speaking simply with -nH will not create catalog for each domain.

Configure .htaccess with mod_rewrite one level up approach

The structure of my project is
/var/www/mysite
------pages
------scripts
------other
In it's corresponding virtual host the configuration is:
ServerName mysitename.com
DocumentRoot /var/www/mysite/pages/
Because of that, when serving the index.php from the pages folder, the resources(scripts/images etc ) are not found, as expected, because the page is trying to load them like so. http://mysitename.com/scripts/storage.js.
This of course makes sense.
How would you approach solving this? I am aware that by setting some mod_rewrite rules you can conditionally rewrite urls, is that a way to go about it? Im mainly interested in seeing what my options are here rather than getting one solution like, move your .html file up one layer.
There are 2 possible solutions:
Using alias in your Apache server config (of course you need to have control over Apache config). An example of alias command:
Alias /scripts /var/www/mysite/scripts
<Directory /var/www/mysite/scripts>
Options Indexes
Allow from all
</Directory>
Create a symbolic link inside pages/ directory. For example on *nix systems use this command to create symlinks:
cd pages
ln -s ../scripts .
ln -s ../other .

mod_rewrite on the same server but different website

I have 2 websites on a server located in the following folders:
/var/www/site1.com/
/var/www/site2.com/
There is a folder 'test' on 2nd site with php-scripts inside:
/var/www/site2.com/test/
So these scripts could be accessed via http this way:
http://site2.com/test/script.php
I need these scripts to be also access via 1st domain, like this:
http://site1.com/test/script.php
BUT without copying these files to site1's folder
is this possible using mod_rewrite?
Just make a symlink to your test folder.
ln -s /var/www/site1.com/test /var/www/site2.com/test
If you have the +FollowSymLinks option in your apache configuration for that vhost, I guess that should work.

Tricky Apache redirect/hack wanted to redirect site.com/dir/ to site.com/dir/foo (where foo is the only file in /dir/ (but its not always named foo))

There is a section of my site that's setup for easy posting/sharing of files, each time a file is uploaded it's placed within it's own directory, examples:
http://c.sente.cc/78at/stackoverflow.html
http://c.sente.cc/XEEA/free
I'm curious if Apache can be configured such that any requests to a file's parent directory will automatically be redirected to the file itself:
http://c.sente.cc/XEEA/ -> http://c.sente.cc/XEEA/free
http://c.sente.cc/78at -> http://c.sente.cc/78at/stackoverflow.html
I know an individual .htaccess file could be created within each directory which would explicitly make the redirect but I'd rather not have a million .htaccess files for each of my million uploaded files.
Thanks
I don't think this can be done with stock Apache. mod_rewrite is the tool you would normally use, but to the best of my knowledge it doesn't have any facilities for listing files in a directory.
You could write a little shell script to do the redirect (ex, something like:
#!/bin/bash
base_dir="/var/www/uploads"
cd "$base_dir"
requested="${QUERY_STRING//../}"
requested="${requested#/}"
echo "301 Moved permanently"
echo "Location: /$(ls -1 "requested" | head -n 1)"
but that's just a quick hack, and probably doesn't work), then use a mod_rewrite rule like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) /cgi-bin/redirect?$1

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