Miroslav
Miroslav

Reputation: 29

.htaccess excluding rewrite css and images

I have some problems with rewriting URL-s

my .htacces file:

RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]

my file and folder structure:

bsc/
---|.htaccess
---|index.php
---|css/
-------|style.css
---|images/
----------|logo.png
----------|image1.jpg
----------|image2.jpg
---|controller/
--------------|bootstrap.php
...

When I have next link

localhost/bsc/page.html

Than my css work or I can get to work. But when I have next links:

localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage.html
localhost/bsc/page/subpage/subsubpage/subsubsubpage.html

I can't get css to work, css is redirected to

localhost/bsc/page/css/style.css
localhost/bsc/page/subpage/css/style.css
localhost/bsc/page/subpage/subsubpage/css/style.css

Second part of problem is that my links don't have same extensions or any extension. Next situations are possible:

localhost/bsc/page/subpage
localhost/bsc/page/subpage/
localhost/bsc/page/subpage.php
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage
localhost/bsc/page/subpage/subsubpage/
localhost/bsc/page/subpage/subsubpage.php
localhost/bsc/page/subpage/subsubpage.html
...

Now defined rewrite rule is working for different links but how to add excluded rule for css and images?


I get next .htaccess that work for css for all my conditions:

RewriteEngine On
RewriteBase /bsc/
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]

After reading comments and some time I get next .htaccess file with not so bad results:

RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ index.php?url=$1 [NC,QSA,L]

and my link tag is:

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

Now it is only one condition in URL when I can't get correct URL request. Next URL don't receive last parameter:

localhost/bsc/page/subpage
localhost/bsc/page/subpage/subsubpage

I don't receive subpage or subsubpage, other conditions are working. In this situation apache and .htaccess tray to find file or folders ....

Is it possible to add missing condition or is it possible to make some simplest rewrite rule or more rules

Upvotes: 3

Views: 22530

Answers (3)

Sajal
Sajal

Reputation: 1216

I applied url rewriting to a project in core php. I faced the same problem. css, Js and images were not getting loaded. I used this and it worked for me. Source

# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

Upvotes: 4

Tehnix
Tehnix

Reputation: 2040

You probably just need to use an absolute url for you css,

i.e.

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

instead of

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

Putting a slash infront forces it to the "root" of the url, thus making it consistent throughout subpages.

Upvotes: 9

Wrikken
Wrikken

Reputation: 70490

  1. Add the condition RewriteCond %{REQUEST_FILENAME} !-f before your rewriterule.
  2. You should include css with src="/css/style.css" and NOT src="css/style.css" (note the missing starting /.

Upvotes: 0

Related Questions