Fulanotal Games
Fulanotal Games

Reputation: 63

Redirect index.php does not load css with slash in PHP url parameters

I'm using .htaccess to remove index.php from the URL and use its parameters with a / (slash).

For example, before:

example.com/index.php?id=posts

after:

example.com/posts

I'm also using .htaccess to allow / (slash) in the parameters like: example.com/posts/mypost, where posts / mypost is an index.php parameter!

But my problem is that when I access the parameter with a / (slash) like: example.com/posts/mypost it loads the page but without loading the CSS, it only works if I use without the / as in example.com/posts or just a string with no bars! But if I DELETE this part of the .htaccess code:

RewriteCond %{THE_REQUEST} ^.*/index\.php

RewriteRule ^(.*)index.php$ /$1 [R=301,L]

The parameter with / slash works, however using index.php?Id= like: example.com/index.php?id=posts/mypost. But it's not what I want, I want to remove the index.php and use the parameters of him with / slash!

My .htaccess complete code:

#remove index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

#use parameters without " ?id= "
RewriteEngine on
RewriteRule ^/?index.php$ - [L,NC]
RewriteRule ^([a-zA-Z0-9.]+)?$ index.php?id=$1 [QSA,L]

#allow " / " slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !-d 
RewriteRule (.*) index.php?id=$1 [L,NS]

Upvotes: 0

Views: 714

Answers (1)

imvain2
imvain2

Reputation: 15847

When using:

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

And your URL is: example.com/posts

your browser is looking for: example.com/posts/css/style.css

By simply changing it to:

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

No matter how many folders deep you go, the browser will always go to the root for the file.

so example.com/posts will look for example.com/css/style.css

so example.com/posts/more/and/more/and/more will still look for example.com/css/style.css.

Upvotes: 2

Related Questions