htaccess redirect non-existent directories to index.php

Posted July 4, 2019 12.1k views
UbuntuApacheLAMP Stack

Goal: have redirect to
Issue: 404

I’ve meticulously followed:

Activate Mod_Rewrite ✓
Create new .htaccess file ✓
Allow use of .htaccess ✓
Restart Server every change ✓

My current .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /

Misc. Fun Facts:

  1. I have multiple domains on the same droplet: var/www/, var/www/, etc.
  2. One other domain is using an .htaccess file in its respective folder (conflict?)
  3. I have tried keeping .htaccess file inside public_html and inside level
1 comment
  • The tutorial link above was supposed to be this DO article, instead

    by Etel Sverdlov
    This tutorial goes over Mod_Rewrite, an apache module that allows you to manipulate URLS to provide shorter or more relevant ones. This tutorial will go over Activating Mod_Rewrite, Creating and Using the required .htaccess page, and setting up the URL rewrites.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers


What I could suggest is first to make sure that your .htaccess file actually works. You could try causing a 500 error by adding some random characters on top of the file (Example: just pot asd on the first line of the .htaccess file and save it) then try accessing your site, if you get a 500 error then you would know that the .htaccess file actually works.

If this is the case, you could try modifying your redirect rule a little bit:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

If the .htaccess file is not working you would need to check your Apache config again.

Hope that this helps!

  • Thanks for the consideration Bobby, still no luck.

    Update 1: if I do - it successfully redirects! When I do /about it 404s.

    Note: I have an about.php file that you can view if you add the .php extension, but I want to be able to force /about to still show index.

    New things I’ve tried:

    Confirmed .htaccess is functioning ✓
    - 500 error on “asdf”
    Tried your redirect code ✓
    Renamed my other website .htaccess file ✓
    Triple checked Apache Config ✓

    <VirtualHost *:80>
        <Directory /var/www/>
            Options Indexes FollowSymLinks Multiviews
            AllowOverride All
            Order allow,deny
            allow from all

    Ran apache2ctl configtest (no error) ✓

    • Hello,

      Thanks for the clarification.

      In this case can you try with the following in your .htaccess file:

      ErrorDocument 404

      Let me know how it goes!

I am facing same issues, and not able to get any resolution.
please share, how you fixed this issue.

#.htaccess file to redirect all non-existent paths and files to "/index.php"

<IfModule mod_rewrite.c>

   RewriteEngine On

   RewriteCond %{REQUEST_FILENAME} !-f [OR]
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php [L,QSA]