0

I use .htaccess to rewrite urls. See the example below :

my folder arborescence is like that :

  • mysite (folder)
    • css (folder)
      • style.css
    • mypage.php
    • .htaccess

when i call http://localhost/mysite/mypage/myid it is redirected to http://localhost/mysite/mypage.php?id=myid

The problem then is that it doesn't seem to call my css which is :

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

The content of my .htaccess is :

RewriteEngine on
RewriteBase /mysite
RewriteRule ^mypage/([^/]*) mypage.php?id=$1

Thanks in advance

Edit :

I tryied with the solution :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

I still cannot get the css called :/

Edit 2 :

Ok i could get it work calling my css like this :

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

Looks like i also have to call all my images i have in the html using the path "/mysite/"

3 Answers 3

1

Your styling get's called relative now, so you should call it absolute.

There is no /myid/css/ folder, that's what your code is looking for now.

<link href="<?php echo $_SERVER['SCRIPT_URI'];?>css/style.css" rel="stylesheet" type="text/css" />
Sign up to request clarification or add additional context in comments.

3 Comments

Is the result of that function supposed to write <link href="localhost/mysite/css/style.css"> ? Because i get php error using that function (im on windows Wamp srv). Thank you
Yes, on a live server, it will echo the complete uri of the current page. But because you are running on localhost, it doesnt work somehow. it should say (http://)localhost/mysite/css/style.css.
ok, so i also have to add this function for all the <img src=""> right ? Isnt there an other way to do it ?
1

You need to tell the rewrite engine to ignore directories and files that actually exist:

RewriteEngine on
RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mypage/([^/]*) mypage.php?id=$1

Comments

1

add a rewritecondition:

RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mypage/([^/]*) mypage.php?id=$1

now your css (and other files and directories) will not be sent trough the rewriterule

Comments

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.