William
William

Reputation: 63

How to remove extension .php in the middle of URL

Here is my URL: http://localhost/school-project/project1/mypage.php/home

I wanna get rid of .php in mypage.php. So the new URL should look like this: http://localhost/school-project/project1/mypage/home

I have tried to use RewriteRule in .htaccess, but none of them worked!

Here is the code in my .htaccess:

(this one actually gets rid of the .php, but it turned the page to Object not found, error 404)

RewriteRule ^mypage.php/(.*)$ http://localhost/school-project/project1/mypage/$1[NC,L,R]

or

RewriteRule (.*)mypage/(.*)$ /mypage.php?/$1 [L]

I don't really know where the problem is. Any ideas? Thanks!

Upvotes: 1

Views: 599

Answers (2)

William
William

Reputation: 63

I found another solution for my problem. Thank you so much for all the help!

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^mypage(/.+)$ mypage.php$1 [NC,L]

Upvotes: 1

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Try this :

RewriteEngine on 
RewriteCond %{THE_REQUEST} \s/+(.*)\.php(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_URI} !\.php
RewriteRule ^([^\/]*)/([^\/]*)$ $1.php/$2 [L]

Second & third lines are removing php extension externally. Forth & fifth to redirect request to original path internally .

Clear browser cache then test , if it is Ok change R=302 to R=301 in order to be permanent redirection .

Upvotes: 2

Related Questions