Redirect subfolder requests to subfolder - apache

I have a setup where I can only access the root of a domain (subdomains are not allowed due to restrictive firewall policies). Lets say the domain is www.domain.com, for testing i have a symlinked a folder in /var/www/testing that points to several html and css files. In these files, scripts and css are included like e.g.
<link rel="stylesheet" href="/css/app.css">
which will fail as apache looks for the file in www.domain.com/css/app.css while it resides in www.domain.com/testing/css/app.css is there a possibility with .htaccess or RewriteEngine to make all requests (for css and js files) coming from /testing/ also going to /testing/... like examplified above?

Try
RedirectMatch ^/(css/.+\.css)$ /testing/$1
This will redirect /css/file.css to /testing/css/file.css

Related

Redict Apache Directory Index

I've got an Apache server, and I'd like to set it up such that when a directory is requested that does not have an index.html file (and thus, Apache would, by default, generate a directory listing), Apache instead redirects (ideally using HTTP code 303) to a given url.
Unless absolutely necessary, I'd like to stay away from going outside Apache (for example, by having Apache load a php script which writes the headers manually). This is an otherwise static site, and I'd like to avoid having to introduce scripting languages into the mix.
Also, note that this post doesn't solve my problem since all of the proposed solutions use external scripts.
So I figured out that by using a combination of HTML meta refreshing and JavaScript redirection, I could cover almost all browsers in use and still have a static file. So what I did was this. In the apache site config, I put a directive that told apache to first look for index.html files, and if that failed, use a site-wide /no-index.html:
<Directory /path/to/web/root>
DirectoryIndex index.html /no-index.html
</Directory>
no-index.html, then, contained the following:
<html>
<head>
<meta http-equiv="refresh" content="0; url=/">
<script type="text/javascript">
window.location = "/";
</script>
</head>
</html>
(in this example it redirects to the web root, /, but you could replace that with whatever url you wanted)
See here for an explanation of what the <meta> tag is doing.

.htaccess, placed inside subdirectory, to point paths to said subdirectory

Edit: CONFIRMED NOT POSSIBLE
I have a static HTML site where all references are absolute, based on the document root being /
eg.:
<img src="/images/myimg.jpg>
<link href="/styles/main.css">
and so on...
For testing purposes, I need to be able to put the site inside of a sub-directory and point all paths to that directory.
ie., the paths need to effectively be this:
<img src="/sites/myvsite.com/images/myimg.jpg>
<link href="/sites/myvsite.com/styles/main.css">
and so on...
The catch is that my .htaccess file needs to be in /sites/myvsite.com to because I can't modify files in the document root.
Is this possible with mod_rewrite or some other .htaccess facility?

What HTTP header indicates when Index.html has been served?

Excuse the awkward title: I'm building a simple web server (don't ask...) and have this problem:
The browser requests mydomain.com/MyFolder
My server spots this is a folder, so instead, delivers mydomain.com/MyFolder/index.html
All fine so far, except that index.html has link to mycss.css, but the browser requests it as a top-level file mydomain.com/mycss.css instead of mydomain.com/myFolder/mycss.css.
Is there some HTTP header that needs setting up to indicate that a different page has been served? I've tried returning Content-Location: /myFolder/index.html, but without any visible success.
index.html basically contains this:
<link rel="stylesheet" href="mycss.css" />
Return a 301 Moved Permanently status code, instead of the 200.
Provide a Location header pointing to the same url plus a slash in the end /
Like so:
Location: mydomain.com/MyFolder/
Do not serve the index.html file on that same request, wait for the browser to request again with the slash at the end.
Or just try to add something like this to your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)$ myFolder/$1
Can you just try to change this:
<link rel="stylesheet" href="mycss.css" />
to this:
<link rel="stylesheet" href="/myFolder/mycss.css" />
?
Ok, actually the information given was not correct so I'm modifying the answer. If you are implementing a web-server, you must follow the standard specification. The definition <link rel="stylesheet" href="mycss.css" /> shall retrieve the the CSS file from the same location as the file in which it is defined in (index.html) is. When using relative paths, it is not the browser that requests from a specific location but the web-server should determine the location from which to serve the resource.
Check section 2.4.6 and 3 in the standards document: http://www.ietf.org/rfc/rfc1808.txt
In other words, if the path of a resource does not start with the slash (/), it is considered as relative and should be located relative to the base URL.

mod_rewrite multiple fake subdirectories

Sorry if this is a really dumb question - I can't find the answer anywhere...
I'm storing my test files in a subdirectory called 'dev', and using the following rewrite rule so that requests to /dev/VAR1/VAR2 will take me to /index.php?page=VAR1/VAR2, and requests to /dev/VAR1/ will take me to /index.php?page=VAR1
(VAR1 and VAR2 are variables, and the folders/subdirectories won't really exist)
RewriteBase /dev
RewriteRule ^([A-Za-z/\-]+)/$ index.php?page=$1
This is doing it fine - my index.php is getting the right variables. The problem is that the server or page (or I don't understand what...) thinks I'm actually in the non-existent folder (or subdirectory) which is VAR1, so all my relative (if that's the right word) addresses for images and links and css and scripts etc
<link rel="stylesheet" type="text/css" href="./sitefiles/mystyles.css" />
all work as if they were /dev/VAR1/sitefiles/mystyles.css - basically, the server thinks it's actually in the 'fake' folder.
Is there a way I can continue to use these relative addresses with my mod_rewrite, or do I have to use 'absolute' addresses with the complete path?
Thanks for any help!
We're using the same stuff with our configuration and rewriting fake aliases to index.php?something=$1 we have to put our CSS files into the /styles/ of the DocumentRoot and don't use relative ./ path use the absolute path like href="/sitefiles/mystyles.css" it should work fine.

HTML Base Equivalent in .htaccess?

Let's say you have the following setup.
You have a server with the basepath set to the normal webroot.
The server has files in the following structure
/projects
/some-unique-id
/index.html
/images
/some-unique-id
/index.html
/images
Is it possible to have a .htaccess file somehow redirect the paths so if index.html has absolute paths they work.
For example if index.html contains /images/foo.gif can this magically become /projects/some-unique-id/images/foo.gif
The equivalent of the base html tag.
This is part of a CMS deliverable previewing system so I am restricted to not changing the HTML code.
Is there anyway to address this in an .htaccess file?
If each index.html contains <img src="/images/foo.gif"/>, the browser will request that URL and there will be no way for the server to know which page caused the request since the requests will be identical regardless of the originating page.
There is no way to do this with mod_rewrite unless you try to check the Referer header, which would be unreliable at best.
You will have to change your HTML markup to solve this. Using relative URLs seems like it would solve your problem.