Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewrite url breaks css/javascript/other files

Tags:

.htaccess

my .htaccess file looks like:

RewriteEngine On
RewriteRule ^([^/]*)$ index.php/?node=$1 [L]

now when I include my css file with:

<link href="sitewide.css" rel="stylesheet" type="text/css" />

it doesn't work. The same goes for javascript.

like image 415
Dylan Cross Avatar asked Dec 27 '22 02:12

Dylan Cross


2 Answers

I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...

So here it goes, add this between the <head> tags of the pages you are having problems:

<base href='http://www.mydomain.com/'>

This will make that your relative path links will start with this base link. Simple as that.

The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.

Did it work for you?

like image 105
luiscabus Avatar answered Jan 13 '23 14:01

luiscabus


Consider rewriting only non-existing paths, e.g. if file or directory exists - don't rewrite it.

Wordpress uses this to rewrite their permalinks, I think it's pretty good example:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

As you can see they have rewrite conditions to exclude existing files/directories.

like image 25
valentinas Avatar answered Jan 13 '23 16:01

valentinas