Question

htaccess redirect non-existent directories to index.php

Goal: have www.website.com/anything redirect to www.website.com Issue: 404

I’ve meticulously followed: https://www.digitalocean.com/community/questions/htaccess-and-subdomain-issues

Confirmed: 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/site1.com, var/www/site2.com, etc.
  2. One other domain is using an .htaccess file in its respective site.com/ folder (conflict?)
  3. I have tried keeping .htaccess file inside public_html and inside site.com level
Subscribe
Share

The tutorial link above was supposed to be this DO article, instead


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

#.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]

</IfModule>

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

Hi,

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! Bobby