0

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]
7
  • When calling your css file do you have a slash at the beginning of the path or are you using an absolute path? Commented Jan 11, 2019 at 16:30
  • @imvain2 It does not have slash, my problem I think is that it goes to an alternative path, so it does not load the includes, for example when I enter mydomain.com/ posts/mypost it's like if posts were a path and not "posts/mypost" a parameter Commented Jan 11, 2019 at 16:37
  • 1
    If you are calling your CSS file without a leading slash and without it being called as an absolute path, the CSS file is being loaded as a file from the path in the address bar. Commented Jan 11, 2019 at 16:44
  • Im using <link rel="stylesheet" type="text/css" href="css/style.css"> Commented Jan 11, 2019 at 16:48
  • Check the network traffic in the browser - what is the URL of the CSS file the browser is requesting (for which you are presumably getting a 404)? Commented Jan 11, 2019 at 17:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Still the same :/
After almost a day I solved the problem, you helped me damn, thank you brother: D could you edit your answer so I accept it and give an upvote? I had to use /mydomain/css/style.css

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.