1

I'm currently running this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

But when an inner .html file requests a CSS or JS file, I'm getting an error "resource not found" and the URL to the file looks something like:

www.domain.com/the-inner-page/js/script.js

instead of

/js/script.js

Can I add something to the .htaccess file to avoid this issue?

Thanks!

4
  • What does your html look like? Commented Dec 29, 2015 at 21:05
  • <link rel="stylesheet" href="css/stylesheet.css"> Commented Dec 29, 2015 at 21:06
  • add a / in front of css. It will try to find it in your current directory if you do it the way you're doing it. Commented Dec 29, 2015 at 21:06
  • When you write a href link starting with a /, that means that the link is a relative link and not a full link. You have to use full links or use ../ in the inner pages Commented Dec 29, 2015 at 21:07

2 Answers 2

3

Add this to the head section of your html. And then you can leave your css files with relative URL.

<base href="/" />
Sign up to request clarification or add additional context in comments.

6 Comments

I would advise against this because he may have some files he wants to be relative to the file.
@Jacques No because of his rewrite, the path changes and all of his resources will now show relative to the rewritten directory. He will get 404's for his assets. He now needs to either make them absolute paths, use a leading slash or use base tag which covers it all.
Actually, my statement is still correct. If he has a directory with the same name as the file name where he stores a special css or js file he only needs to load for that page, he would then have to write the path from root, as opposed to writing it relative to where he is in the file. That includes if there is an href to a child page, IE: www.domain.com/the-inner-page/child-page/. With using the base href, he has to write the url as /the-inner-page/child-page instead of just child-page.
That's not what his problem is he said it's from the root. /js/script.js You are making up scenarios that don't exist and that he has not stated was a problem. So for his current issue that is the solution. This a very common issue with rewrites and I have answered this same issue dozens of times.
"You are making up scenarios that don't exist", no I'm being realistic. I'm saying to use root relative urls only for things that are root relative instead of everything, and used possible scenarios that are very common with websites.
|
0

The rewrite is not the issue. Either update your reference to the css to be root relative (start with a / IE: href="/path/to.css") or you can use <base href="/"> as Panama Jack stated.

I would advise using root relative, as it doesn't prevent you to write all relative urls as root relative, just the ones that need to be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.