Url rewriting broke css link

I use the following parameter to rewrite the URL:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

In index.php , $_GET['url'] is parsed, so in the following examples:

 ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome 

So the first parameter (? I don’t know what to call it) is the name of the page, then the following pair of parameters is similar to "keys / value". Now, when I look through the ROOT/ link to the stylesheets inserted inside the html page, and the page displays correctly. I look at ROOT/index (this is the same as ROOT/ ), it shows the page (with content and other material) correctly, but links (even if correctly written in the html structure) are not loaded into the style sheets. And I see this because my page does not have CSS at all when I load it.

How can i fix this?

EDIT

The css file path is as follows:

 project/view/css/common.css 

The file it is in is located in

 project/public/index.php // the one with .htaccess and rewrite rules 

This leads me to create a link (inside index.php) like

 ../view/css/common.css 

But it works depending on how the URL looks. Examples:

 # For URL = public/ project/view/css/common.css // good # For URL = public/index/ project/public/view/css/common.css // broken # For URL = public/index/key/value project/public/index/key/view/css/common.css // broken 
+2
source share
3 answers

I think you have two problems (as these answers have not yet solved your problem ...):

  • You rewrite all the file names, so when the browser asks for css/index.css , your rewrite code is converted to index.php?url=css/index.css . @ cOle2 answer should solve this.

  • You are not using absolute paths for your css files. The server translates the requested page, for example, /user/id/1/name/bobby to /index.php?etc.... , but when the browser requests, for example, a css file, something like css/index.css in your In the code, it really requests /user/id/1/name/bobby/css/index.css , and this file does not exist. You can solve this using only absolute paths to your external files (css, js, images, etc.), e.g. /css/index.css .

Edit:. Based on your comments, both paths are relative and your css is inaccessible by the browser, so you need to:

  • Move css to the project/public directory (e.g. project/public/css )
  • Use absolute paths to your css, e.g. /css/index.css .
  • Make sure the URLs of your external files are not rewritten by the server.
+3
source

The comment does not allow me to format the code, can you try:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC] RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

Update

You can try something like this in the <head> section of your pages to support the relative path for your image / css / js files listed on this page:

 <head> <base href="http://www.example.com/static-files/" /> </head> 
+7
source

You can try to remove certain file extensions from the rewrite rule, for example, the following rule will ignore images, css and js files.

 # Do not process images or CSS files further RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L] 

Edit: this is how I would apply it:

 RewriteEngine On RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
+2
source

All Articles