1

I am writing the following rules in htaccess file to change the query string as directory structure

 RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1

is used to rewrite the url. It is working fine on

localhost/project/dashboard // (dashboard.php)

and all links are as

localhost/project/css/style.css
localhost/project/js/script.js

But When I append an id

localhost/project/dashboard/1 // (dashboard.php?user_id=1)

It changes all the links as

localhost/project/dashboard/css/style.css
localhost/project/dashboard/js/script.js

Why it is appending the dashboard to all links

2 Answers 2

1

How is the style.css referenced in your html file?

If you have it like this href="css/style.css", the HTML doesn't know you're rewriting, thinks /1 is a folder and will look in dashboard/css/style.css

Any system that uses url rewrite usually has to write all the path to the styles and scripts to avoid this. so you will have to reference your style like this href="http://localhost/project/css/style.css"

if you have a production and development environment it will help you to have a variable like

if($SERVER['SERVER_NAME']=='localhost'){
  $BASE_URL = "http://localhost/project/"
}else{
  $BASE_URL = "http://mydomain.com/"
}

and put that before any call to css, scripts or images ;)

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

Comments

1

It's because you "tell him" to do that.

RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
         //  ^here you tell him to print that "dashboard"

Of course other links works - you don't even match them with that rule.

I found you something here, scroll down to the title "Strip Query Strings". There, it say you to do this:

RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? [R=301]

Just, of course, change that url to your own.

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.