HTML Base Equivalent in .htaccess? - apache

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.

Related

Shutting down a website using HTaccess does not work

OK, it's very simple but it does not work. I have a wiki site where the root contains an index.php file and the subdirectories contains the content of the wiki (I use PMwiki, so no database is required)
I want to temporarity shutdown the website and make it unaccessible by using an nice HTML page to display the shutdown message. I could rename the index.php file, but the rest of the files in the subfolder will remain accessible.
The first thing that worked but which is not elegant is restricting the whole site with a password in the htaccess using "Require valid-user" and all it's other command. The problem is that I cannot display a nice shutdown message as an HTML file.
Else I tried renaming the index.php file to something else like site.php. Creating a index.html file as a message and using a script like this:
Order Deny, allow
Deny from all
<File "index.html">
Allow from all
</File>
In that case, the index.html file is accessible, but it must be manually typed in the URL, it will not use this file by default. I tried adding DirectoryIndex directive like this
DirectoryIndex index.html
But it still does not work.
So first is there a way to make the user only see 1 page in particular and block everything else.
Second, doing so makes the site unaccessible to me. So is there a way to passords restrict the whole directory structure except for a specific index.html file. So that I could type url/site.php and be able to enter my website using an htaccess password.
Thanks for any help
Just this rule in root .htaccess should be able to handle this:
RewriteEngine On
RewriteBase /
RewriteRule !^shutdown\.html$ shutdown.html [L,NC]
Now you can keep custom HTML content in /shutdown.html. Keep in mind you need to use inline css/js since it will also rewrite css/js requests to /shutdown.html file.

Apache cross domain 404

I want to make apache to always open up a signle page for 404 errors from all subdomains.
The problem is that my subdomains are located in subfolders in public_html, and thus have a different root path.
For example the main domain this works quite well:
ErrorDocument 404 /Error/404.html
The Error folder and the main domain are located in public_html respectively.
However for the forum subdomain, located in public_html/forum/ the above root path does not, and it actually looks for public_html/forum/Error/404.html which doesn't exist.
I tried to rewrite rule for the forum folder, but it didn't work out either:
ErrorDocument 404 /../Error/404.html
Seems, it cannot go below the root folder for some reason.
Any ideas how can I refer to the same page from the main and the subdomain alike, without triggering redirects? (eg: http://mysite/Error/404.html would accomplish this, but would also change the url address of the page which I don't want)
Seems, it cannot go below the root folder for some reason.
Because being able to traverse above the document root is a very, very serious security risk. If your webserver gets compromised, people would be able to serve all kinds of files anywhere on your entire server.
If you have access to server config you can setup aliases for the /Error folder. For example, in your forum subdomain's vhost config, you can add:
Alias /Error /path/to/public_html/Error/
This way, when you go to http://forum.yourdomain.com/Error/404.html you'd actually be looking at http://main.yourdomain.com/Error/404.html. Then you can just use:
ErrorDocument 404 /Error/404.html
like normal in your forum subdomain.
But if you don't have access to your server/vhost config, you'll need to use mod_proxy and mod_rewrite. So in the htaccess file in public_html/forum/, add these to the top:
RewriteEngine On
RewriteRule ^Error/(.*)$ http://main.yourdomain.com/Error/$1 [L,P]

.htaccess file on xampp (basedir wrong.. incorrect path for images, css files..)

i got another problem.
i have an own apache server (XAMPP) on my computer. The URL in my browser looks like
http://localhost/pageExample/index.php
i use a .htaccess file to change my url from ?action=home to home.html
The problem now is, that the path to all images, css files etc be wrong. They looks like
http://localhost/images/logo.jpg
I think, there is something wrong in my .htaccess file... i tried <base href="http://'.$_SERVER['HTTP_HOST'].'/pageExample/">.. this works ..okay... but some scripts with extern images doesnt work.
My .htaccess file looks like:
RewriteEngine On
RewriteRule ^home.html$ /pageExample/index.php?action=home [L]
(By the way... the problem came since I created the .htaccess)
How to change this?
Thanks!
This is a common problem when you start rewriting URLs but website is not coded accordingly. In your HTML/CSS, when you are referring to images/scripts/css resources you most likely have something like this: <img src="images/hello.jpg" />. Bacuse you are rewriting URLs you need to alter the resource URLs as well -- in the above example resource is linked to the path of the HTML file (relative path) -- you need to make the path absolute. For this -- add leading slash before resource references:
change
<img src="images/hello.jpg" />
to
<img src="/images/hello.jpg" />

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/

Can Apache serve a default file instead of a 404?

We have Apache serving a set of files from a particular directory. If a file requested in that directory does not exist, can I make Apache return a default file - instead of a 404? If so, how?
This can be achieved making use of .htaccess.
Check this WebReference article on the topic.
Basically you only need a file name .htaccess in your directory which contains
ErrorDocument 404 /YourCustomErrorPage.html
Of course any other file can be served too.
What kind of file? Is it a static file? With Apache you can set custom error pages.
Be careful returning non-404s for any random URI request. It may impact your Google rankings if they notice it and consider it part of a link farm or other such blackhat SEO technique.
If you want to return a file successfully instead of with a 404 status, you can use the FallbackResource directive instead of ErrorDocument.
FallbackResource /YourCustomDocument.html