Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

Php to avoid url ?

What is a better way to avoid the .php extension in the browser url , set a specific path to it according to the work?

0 likes
10 replies
Sinnbeck's avatar

You have a an index.php file that handles all requests. This then gets the path from the current url, and loads the correct data. This is how most php frameworks work (laravel included) :)

The path is available by doing

$uri = $_SERVER['REQUEST_URI'];
1 like
Shivamyadav's avatar

@Sinnbeck Its giving me this /forms/student/create.php and i want to remove .php form this uri?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Shivamyadav Seems that you have a file at that path and are calling it directly. Call a path and let index.php handle it. LIke /form/student/. Be aware that it cannot be the same as an existing folder

If you just want to call the files directly but leave out the extension, you can perhaps do something in your webserver. What are you running? Apache or nginx?

Shivamyadav's avatar

@Sinnbeck used this method by loading everything with the index.php file preety good.. Thanks sir :)

Shivamyadav's avatar

@Sinnbeck I have an other error with date value in php error Uncaught mysqli_sql_exception: Incorrect date value: '' for column 'dob' at row 1

Sinnbeck's avatar

@Shivamyadav that seems unrelated to the topic of this thread. Please make a new thread and post the related code

azimidev's avatar

To avoid having the ".php" extension in the URL, you can use URL rewriting. This is a technique that allows you to map a URL to a file on your server without having the file extension appear in the URL.

Here's an example of how you can set up URL rewriting using Apache server and the mod_rewrite module:

Enable the mod_rewrite module in your Apache server:

sudo a2enmod rewrite

In your .htaccess file, specify the rewrite rules:

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

In your index.php file, use the $_SERVER['REQUEST_URI'] to determine the path and route the request accordingly:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

switch ($path) {
    case '/home':
        // Render the home page
        break;
    case '/about':
        // Render the about page
        break;
    ...
}

This will allow you to set a specific path to the file according to the work, without the .php extension appearing in the URL. For example, http://example.com/home would load the home page, while http://example.com/about would load the about page.

Please or to participate in this conversation.