0

I am trying to route a single blog post from a url

The url looks like this : http://localhost/news/post/1/post-title-slug

And my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]`

And my routing file I am currently using a library called nezamy/route

$route->get('/post/?/?', function($postid,$page) {
    include 'post.php';
});

When I do this my css, js and images is renamed as well to something like this: http://localhost/news/post/1/css/main.css

1
  • That is because you load the css, js files from the relative location. You need to make a function to get the base url taking the rewritten path as a parameter and load css, js files from that base path. Commented Jun 23, 2017 at 8:16

2 Answers 2

1

You need to add the css/js files with their absolute location when using htaccess rewrites. To do this, you can define your base url in a php variable as shown below:

$base_url = 'http://localhost/';

and then refer to it where you call the css/js files like so

<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?>css/main.css">
Sign up to request clarification or add additional context in comments.

Comments

0

Prateek's answer is good, but I solve similar problem by adding.

<base href="/"/> 

in my head tag.

or you can experiment with different folder like:

<base href="/web/"/> 

and etc.

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.