0
<?php
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$url_array = explode("/", $url);
$action = end($url_array);

switch ($action) {
    case "login":
        login();
        break;
    case "register":
        register();
        break;
}
?>

Here I need to remove the .php extension from my URL like I have to type like https://example.com/user.php/login for login page and https://example.com/user.php/register for register page.

But I need the URL to be https://example.com/user/login for the login page and https://example.com/user/register for register page and so one

Here I want to remove the .php extension from the user page. Please suggest any code for .htaccess

Brajlal
  • 41
  • 4
  • 1
    Does this answer your question? [Remove .php extension from url](https://stackoverflow.com/questions/9736591/remove-php-extension-from-url) – Andrea Olivato Jul 22 '21 at 12:47
  • 1
    Does this answer your question? [Remove .php extension with .htaccess](https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – JON Jul 22 '21 at 13:49

2 Answers2

2

please try this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)$  https://www.example.com/$1.php/$2 [L,NC,P]

Of course, where you put this rule is also important. Because it is a rule that may contain many links.

I prefer something like the following rule:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/(.+)$  https://www.example.com/user.php/$1 [L,NC,P]
  • The URLs are on the same site so `P` is not required . You can just remove the hostname and scheme from the destination string for internal redirection. – Amit Verma Jul 22 '21 at 17:43
  • Thanks a lot, both is working but when I am using the above code https://www.example.com/user/login is working perfectly but the constant page like https://www.example.com/terms is showing 404 error. – Brajlal Jul 23 '21 at 13:46
0

In .htaccess file, add following lines

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Gaurav
  • 3,615
  • 2
  • 27
  • 50
Fran
  • 1
  • 2
  • Without [P] the link will be redirected and the final address will change,i think in this case we don't want the link to change. – sama latifi Jul 22 '21 at 13:32