2

I was trying a MVC tutorial by howcode on youtube but found that htaccess does not allow my css and js files to load.

.htaccess file:

RewriteEngine On
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]

I tried to find some solutions over the internet but end up generating other errors. I used conditions before the rule:

RewriteCond %{REQUEST_FILENAME} -f

But this doesn't seem to work.

Project Structure -

I have these files in project

  1. Classes (folder)
  2. Controller (folder)
  3. Views (folder)
  4. .htaccess
  5. index.php
  6. Routes.php

In Views folder I have-

  1. css (folder)
  2. js (folder)
  3. index.php
  4. header.php
  5. footer.php

My all the imports are in header.php and header.php is included in index.php

My code for linking css is-

<link href="./Views/css/style.css" rel='stylesheet' type='text/css' media="all" />
4
  • Are you using codeigniter? Commented Jul 17, 2017 at 19:53
  • @MuhammadUsman no, I am not using codeigniter or any other framework Commented Jul 17, 2017 at 19:58
  • Check your network tab in your browser's dev tools. What are the return codes for the CSS/JS files? Are the path's correct? (this is with the RewriteCond Commented Jul 17, 2017 at 20:36
  • @JonLin It is not showing any path errors but it is showing that MIME type of css file text/html rather than text/css. I don't think that it is an issue because it is working fine without htaccess Commented Jul 18, 2017 at 5:32

1 Answer 1

2

I used conditions before the rule:

RewriteCond %{REQUEST_FILENAME} -f

That condition is the opposite of what you are trying to achieve - the logic is reversed. That condition tests if the file exists and only if it exists then process the RewriteRule. You need the rule to execute only when the requested file does not exist. For example:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]

Note the ! prefix on the CondPattern. This negates the condition.


UPDATE: Note that the above RewriteRule pattern (ie. ^([^/]+)/?) excludes requests for the document root (ie. http://example.com/ - an empty URL-path). So, the url GET parameter will indeed be undefined in your PHP script for this request. However, this should be OK, providing you are checking the existence of this variable before using it (as you should be). For example:

$url = isset($_GET['url']) ? $_GET['url'] : null;

If you explicitly want this parameter to be set (and your directive processed) for such requests (although unnecessary) then you would need to change the RewriteRule pattern from ^([^/]+)/? to ^([^/]*)/?, ie. change + (1 or more) to * (0 or more).

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

8 Comments

I used this but it gave me error in php that "Undefined index: url"
What URL are you requesting? Using the RewriteRule pattern in your question, url won't be set if you request the document root ie. http://example.com/, but should be OK if requesting a positive URL-path, ie. http://example.com/something. If you specifically need this to match the document root (although unnecessary) you can change the pattern from ^([^/]+)/? to ^([^/]*)/? (ie. change + for *). However, this highlights a bug in your PHP code: you should always check the existence of a URL $_GET parameter before using it.
It did not worked. Now it is not giving any errors but still no css
How are you referencing your CSS files in index.php? What URL are you using?
And what URL are you requesting?
|

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.