Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect hiding the php extension or pass a parameter

I'm using a main index.php file to handle the most of requests, so my basic .htacess looks like this one:

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

But i've got some requests (like /signin or /signout) that needs to be handled directly from .php file with the same name (/signin.php and /signout.php).

So my .htaccess rules needs to be: "if %{REQUEST_FILENAME}.php does not exists redirect to /index.php?page=$1 else redirect to %{REQUEST_FILENAME}.php".

How can i do this?

like image 942
Alvins Avatar asked Dec 05 '25 13:12

Alvins


1 Answers

Replace your code with this:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]

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

This will send everything to /index.php that is not a valid file or directory and doesn't have a matching .php existing in your filesystem.

like image 94
anubhava Avatar answered Dec 08 '25 03:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!