.htaccess trying to use slashes without redirect 404 error

245 Views Asked by At

I am trying to prevent a 404 error and redirect when using mydomain.com/something

It throws a 404 error because the page something.php does not exist. I want it to submit to the index page so I can capture the value with php and use it in my app. I also want to combine this with removing the extension (.php) yet still being able to access the pages. For example, instead of mydomain.com/contact.php I want to be able to type mydomain.com/contact but still be able to use mydomain.com/something and have it submit that to the index page if the page something doesnt exist.

Hopefully someone can help me as I have tried a lot of different things and nothing seems to be working. The closest I have been able to get was by using the following htaccess code:

DirectoryIndex index.php
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|css|index|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]

However, it seems like when the file doesnt exist and I just want the value to submit to the index page as a slash, it doesnt work and it hangs and eventually throws a 404.

1

There are 1 best solutions below

0
On

Try this code in your DOCUMENT_ROOT/.htaccess file:

DirectoryIndex index.php
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteBase /

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]