assets relative to root, but html files are in subfolder - .htaccess

I have 675 html files that ahve been passed to me today.
The folder structure is as follows:
- index.html
- /assets/
- /pages/
In the pages/ folder I have the 675 html files. In each of them I have all the assets that are relative to the root folder, like so:
<link rel="stylesheet" href="/assets/css/main.css">
I have uploaded everything to my demo server, and the URL is like so:
https://example.com/CLIENT/rebrand/
The index.html page contains a list of the 675 html files in pages folder.
As you can imagine, when I click on one of them, I get the page, but images, js and css are not loaded.
Is there a way to instruct the server, via .htaccess maybe, to interpret
<link rel="stylesheet" href="/assets/css/main.css">
as
<link rel="stylesheet" href="/CLIENT/rebrand/assets/css/main.css">
I would like to avoid a massive search&replace, in so many files, because the risk is high to break something.
Do you guys have any hint on a cleaner solution? Thank you

You can use this redirect rule at the top of your .htaccess for this redirect:
RewriteEngine On
RewriteRule ^assets/.+$ /CLIENT/rebrand/$0 [L,NC,R=301]
# other rules appear below this line

Related

Redirect all requests to a subdirectory using htaccess

I have a subdomain http://www.sub.example.com but the index file is not in the root directory but instead it is project/example/index.html and some of it's dependencies are there and in the parent directory project/. How can I make it so that users visiting http://www.sub.example.com are able to access the website?
I have tried setting a DirectoryIndex like so:
DirectoryIndex project/example/index.html
But all of my images, scripts, and styles return a 404.
So I tried to rewrite like so:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/project/example/
RewriteRule ^(.*)$ /project/example/$1 [NC,L]
Which fixes my styles and images (which are in projects/example/ directory). However, all of my other dependencies in the parent project/ directory are still missing. Is there a way to include them?
Example index.html dependency in parent directory:
<script src="../node_modules/angular/angular.js"></script>
<link href="styles.css" rel="stylesheet"/>
Add this just below <head> tag of your page's HTML:
<base href="/project/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

.htaccess rewrite images path [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

why is my RewriteRule not working

I want to be able to Rewrite a following url:
www.mysite.com/en/contact
to something like this
www.mysite.com?l=en&p=contact
but keep the structure.
I tried the following RewriteRule:
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?l=$1&p=$2 [L]
but the browser then wants to load all the included scripts like the css from
www.mysite.com/en
How can I tell the browser, that it shouldn't follow the url link?
Or something like that
Thank you for your help
Chris
There are 2 methods to solve this:
1) Use base tag inside head tag
<base href='/'>
2)Use relative links
<link rel='stylesheet' href='/css/style.css'>
Here the browser looks for the CSS file in the folder in the root named CSS, whatever the URL request may be.

How to rewrite certain pages but not CSS, JS etc?

I've been trying for at least 4 hours to fix this problem on my own and have scoured through several hundreds of Stack Overflow pages and other forums in search of a solution, but I can't seem to solve this simple problem.
The contents are arranged as follows:
-- index.php
-- js
- script.js
-- css
- style.css
-- images
- image.png
In the website, I needindex.php?dept=foo to be rewritten as dept/foo. This works with the following RewriteRule:
RewriteRule ^dept/(\w+)/?$ index.php?dept=$1 [NC]
But then the CSS, JS and images won't load any more.
The CSS and JS files are currently are currently included as follows:
<link rel="stylesheet" href="css/style.css" />
I tried changing the URLs to have absolute paths (i.e. /css/style.css too`, but that didn't fix it either).
How can I correctly fix the problem?
Based on your comment have this rewrite rule:
RewriteEngine On
RewriteRule ^dept/(\w+)/?$ index.php?dept=$1 [NC,L,QSA]
Then add this just below <head> section of your page's HTML:
<base href="/live/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
You can keep your CSS path as:
<link rel="stylesheet" href="css/style.css" />

.htaccess rewrite without it affecting relative image/css/js URLs?

I need some simple help with my htaccess file, thank you in advance.
I have a number website URLs, such as:
www.site.com/index.php?page_path=solutions-overview.html
www.site.com/index.php?page_path=solutions-a.html
I want to use the RewriteEngine to change the above links to the following respectively:
www.site.com/solutions/overview
www.site.com/solutions/a
Below is the .htaccess code I am using:
RewriteEngine On
RewriteRule ^solutions/overview index.php?page_path=solutions-overview.html
RewriteRule ^solutions/a index.php?page_path=solutions-a.html
This works, however all of my images, CSS files and JS files no longer load because the page is trying to fetch the wrong URLs. For example "/images/blah.jpg" is instead loading as "/solutions/images/blah.jpg".
How can I modify the htaccess code to prevent relative URLs from changing?
Thank you!
Add this line <BASE href="http://www.yoursitename.com/"> to your page inside the <head> tag as follows:
<head>
<title>Your site title</title>
<BASE href="http://www.yoursitename.com/">
....
</head>

Resources