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

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?

Related

Apache change favicon for certain directory

I'm having a problem with apache that it only looks in the root directory for favicons. I need a separate favicon for a specific directory on my webpage. Below I will draw an example
- favicon.ico
- index.html
- / Folder
- favicon.ico
- index.html
- index2.html
I want /index.html to have a favicon of /favicon.ico and I want /Folder/index.html and /Folder/index.html2 to have a favicon of /Folder/favicon.ico. (Edit: I do not want to specify at the top of each file, there are too many to make that practical)
I tried just putting a favicon.ico inside the sub-folder but it didn't do anything different. Thanks
Simply add the following code to the <head> element of each HTML file were you want to change the icon:
<link rel="icon" href="http://example.com/favicon.png">
The Path needs to be adapted in the code snippet
What is important is that you have this snippet in your HTML files and then you just update the path to the icon you want.
This link might help you:
https://www.javatpoint.com/how-to-add-a-favicon-in-html
Also if you search google for your problem you will get many docs and examples.

Redirect subfolder requests to subfolder

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

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.

.htaccess handling leading forward slash

I have a project which I wish to place in a sub-directory of a web root directory.
So if I had:
/public_html as the web root directory
I want the project to live in:
/public_html/myproject
Now, the project will eventually live in the root folder, so I want to make as few code changes as possible, but during testing it must stay in the sub-directory.
The issue is with the following:
<img src="/images/image1.png" alt="" />
This works fine when the project is in the document root, because the leading forward-slash is doing it's job.
However, when the project is in the sub-directory, the / takes the relative path back to the web root folder.
How can I alter .htaccess to prevent this? I need the leading forward slash to reference the "/myproject" folder instead.
Any ideas on how it can be done?
If you have the apache module mod_rewrite enabled, you could add the following to your .htaccess file in the root folder.
RewriteEngine On
RewriteBase /
RewriteRule ^images(.*) /myproject/images/$1
You can find more information on this here:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

.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" />