htaccess - Load css, js, images - .htaccess

I write htaccess with code:
RewriteEngine On
RewriteBase /MVC_PHP_Basic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?link=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?link=$1&action=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?link=$1&id=$2&action=$3 [L]
And it works and My css,js, image is loaded with url localhost/users but is not load with url localhost/users/detail.
Errors: can't load file css,js.
I know this error is due to path of css,js file because i use:
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap-grid.min.css">
When url is "localhost/users", href is "localhost/assets/css/bootstrap-grid.min.css" (right).
And when url is "localhost/users/detail", href is "localhost/users/assets/css/bootstrap-grid.min.css" (wrong).
Please tell me how to fix (I don't want to use "absolute path").

Try to put / like this "/assets/css/bootstrap-grid.min.css" and place your asset folder on the root folder, or you will have to use like this "/users/assets/css/bootstrap-grid.min.css"

Related

URL rewrite in subfolder css not working

How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory

Rewrite resource URL to serve files from subdirectory

I have a static HTML site that uses resource tags (images, css, scripts) as follows:
<html>
<head><title>htaccess test page</title></head>
<body>
<img src="/img/img.jpg" alt="...">
</body>
</html>
When I load the html file in the browser, the request is made to:
http://localhost/img/img.jpg (which understandably returns a 404)
Where as I'd like the request to be made to:
http://localhost/site/img/img.jpg
The directory structure is as follows:
- www
- site
- .htaccess
- img
- img.jpg
I have been searching for a solution and have a vague idea that RewriteCond is the way to go, but I can't get this to work:
RewriteEngine On
# if requested URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# serve the file from the img directory, yes, very limited,
# and requires me to add rules for scripts, css etc.
RewriteRule ^(.*)$ img/$1 [L]
Any help will be highly appreciated. SO returns quite a few solutions to this issue but none of them seem to work.
You solution is going to treat every file in this way and will re rout it. With this RewriteRule only images will be redirected to /site/{Path of the image defined in HTML}
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ /site/$1 [R=301,L,NC]
You should put images like this <img src="img/img.jpg" alt="...">
The final version looks like this:
RewriteEngine On
# if requested URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# image part
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ /site/$1 [R=301,L,NC]

using mod_rewrite while keeping relative links intact

I am trying to implement clean urls on a website I'm developing.
I'm using .htaccess like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
When the user goes to www.site.com/catalogue, apache is sending them to: www.site.com/index.php?page=catalogue. and that's ok.
But if the user goes to www.site.com/catalogue/item, the site loses the relative links (css, javascript, images).
If the css file is located in css/site.css, apache thinks the html is asking for catalogue/css/site.css.
So: how do I keep the relative links intact while using mod_rewrite?
Also, I would like the solution to be adaptive enough to be able to work inside a folder, ex: www.domain.com/folder/catalogue/item and still refer to www.domain.com/folder/index.php?page=catalogue.
Thank you
There is an easy workaround:
Use absolute path for resource files such as *.css, *.js, *.png, *.gif, *.jpg. ie,
You revise your HTML code from (BAD):
<style rel="stylesheet" type="text/css" href="catalogue/css/site.css" />
=> GOOD:
<style rel="stylesheet" type="text/css" href="/catalogue/css/site.css" />
This requires your re-programming of PHP scripts.

RewriteRule giving 500 error when adding dynamic id at url end

I am getting a 500 Internal Server Error from my linked CSS files etc when I use this htaccess code.. Anyone know what might be the problem? I am not too fluent in htaccess yet.
Here is the code:
RewriteEngine On
RewriteBase /
RewriteRule ^(system|img|res) - [L]
RewriteRule ^picture/([^/]*)/?$ picture.php?id=$1 [L,QSA]
## The below code is something I found on the internet to remove the .php tag
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The URL is supposed to be: www.mysite.com/pictures/1 (id)
The id is always a number.
It does show me the page and I can echo the id, so that part is working, however it gives me a 500 error on linked files as mentioned above.
Not sure why it acts like that tho.. The CSS folder is in the same folder as the actual php file.
You've linked to it using a relative URI:
<link rel="stylesheet" type="text/css" media="all" href="./css/text.css" />
e.g. the ./css/text.css, and while the css file may be in the same directory as picture.php file (which I assume is what is generating the content) but the browser is what actually makes the request for the CSS, not the picture.php script. The browser requests this URL http://www.mysite.com/picture/1, and the server internally rewrite the /picture/1 to /picture.php?id=1, the browser has no clue that's happened. So it sees the base URI as /picture/. If the browser went directly to the php file: http://www.mysite.com/picture.php?id=1, the base URI would be / and the css would resolve just fine to /./css/text.css. But the /picture/1 request has a different base URI so the browser (with no clue that the base is different) blindly attempts to retrieve the css as /picture/./css/text.css, which fails because you have rules that mishandle that URI. Normally you'd just get a 404, but the rules you have after the picture rewrite mishandles the URI and returns a 500 server error.
You can either add in your header:
<base href="/">
in the content generated by picture.php, or make the URI's absolute:
<link rel="stylesheet" type="text/css" media="all" href="/css/text.css" />

how to prevent images and css to be rewrite

I have some questions about htaccess ,
but first of all I want to apologize for my bad English.
I want to ask about RewriteRule .
1.If I want to to change my url extension , for example if I want my url to be mysite.com/file.html but the real url is mysite.com/file.php , how to do that ?
I used this code
RewriteRule ^(.*)\.html$ $1.php [R=301,L]
but when I tried it, I found that .html url redirect to .php
2.when I try to rewrite url from http://mysite.com/file.php?varOne=something&varTwo=somethingelse
to http://mysite.com/file/something/somethingelse/
all css,js and images don't appear bcz the dir changed ,
How can I prevent that ?
Thanks
Have you tried using [L] instead of [R=301,L]?
RewriteRule ^(.*)\.html$ $1.php [L]
And when embedding CSS or images in your html, try to begin with a slash to get the files from the root directory:
<link rel="stylesheet" href="/css/style.css" />
The same with images:
<img src="/images/pic.png" alt="image" />
<img src="/mypic.png" alt="image" />
Add the line:
RewriteCond %{REQUEST_FILENAME} !-f
before all of your RewriteRule lines to prevent redirection for css, js and images.

Resources