0

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!

William
  • 53
  • 10
  • Possible duplicate of [Remove .php extension with .htaccess](https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – Russ J Feb 12 '19 at 15:25
  • Thanks for the comment, it looks similar but I already looked into it, his problem is a bit different. The .php extension is at the end, not in the middle. – William Feb 12 '19 at 15:34
  • See also: [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](//stackoverflow.com/q/20563772) – mario Feb 12 '19 at 16:03
  • "Any ideas?" → Multiviews+path_info should suffice. Else rewrite log. Or look through all the similar questions. There's some more useful explanations buried under the dozens of daily reposts. – mario Feb 12 '19 at 16:06

2 Answers2

2

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 .

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Can you please explain this part \s/+(.*)\.php(.*)\sHTTP.*$ and ^([^\/]*)/([^\/]*)$ $1.php/$2 to me, thank you so much! – William Feb 12 '19 at 16:53
  • The full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1"). This does not include any additional headers sent by the browser. This value has not been unescaped (decoded), unlike most other variables below. – Mohammed Elhag Feb 12 '19 at 16:56
  • more info here https://httpd.apache.org/docs/current/mod/mod_rewrite.html – Mohammed Elhag Feb 12 '19 at 16:56
1

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]
William
  • 53
  • 10