Search code examples
php.htaccesswampwampserver

Why this htaccess file is not working in wamp server


I have this htaccess file it redirects to index.php when url is of type www.mywebsite.com/news but when url is of type www.mywebsite.com/news/2 it shows index.html but no css is applied I am on wamp server

<IfModule mod_rewrite.c>
   RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]
</IfModule>

Solution

  • That's because /news/2 creates a virtual directory.
    Since you're using relative paths for your html links, it does not work.

    Let's imagine you have a css link like this one
    <link rel="stylesheet" type="text/css" href="style.css">
    Since your URI is /news/2 it will search in /news/style.css and that's not what you want.

    Instead, use absolute paths.
    For example: <link rel="stylesheet" type="text/css" href="/style.css"> (with a leading slash, and maybe another base if it's not in root folder).

    Also, it wouldn't hurt to put a RewriteBase / in your htaccess (or a leading slash before index.php)

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ /index.php [L]
    </IfModule>
    

    EDIT: for your project /project_news/ on localhost

    • You have to change all your html links this way (add /project_news prefix) <link rel="stylesheet" type="text/css" href="/project_news/css/style.css">
    • Or, if you don't want to change all links, you can also add <base href="/project_news/"> right after <head> html tag (in all your html pages concerned)

    Then, replace your current htaccess code by this one

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /project_news/
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [L]
    </IfModule>