Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewrite php to php file

I am currently trying to write a rewrite rule. I want to simplify the entered URL so that it will always use the index. The input url would be www.example.com/home.php and would be rewritten to www.example.com/index.php?r=page/home.

I wrote this .htaccess file:

RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]

This configuration unfortunately gives me an error 500. I can understand that it is caused by the fact that if I enter index.php for instance, apache will not know if it must use the index.php file or use the rewritten url index.php?r=page/index.

Is it possible to accomplish this kind of rewriting rule with apache? If so, how can I fix my error 500?

Edit: Please note that the RewriteRule works fine if I change the extension .php to anything else such as .html, as so: RewriteRule ^([^/]*)\.html$ /index.php?r=page/$1 [L]

like image 773
prichrd Avatar asked Apr 20 '26 06:04

prichrd


2 Answers

You have two possibilities.

First one

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]

Second one

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
like image 116
Justin Iurman Avatar answered Apr 22 '26 18:04

Justin Iurman


you can also try with this :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=page/$1 [L]
like image 33
Jinesh Gandhi Avatar answered Apr 22 '26 18:04

Jinesh Gandhi



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!