Current Location: Home> Latest Articles> Pseudo-static PHP Extension Hiding: Implementation and Principle Explained

Pseudo-static PHP Extension Hiding: Implementation and Principle Explained

M66 2025-07-02

Principle of Pseudo-static PHP Extension Hiding

The principle behind pseudo-static PHP extension hiding is achieved through server-side URL rewriting rules. Normally, the server rewrites URLs with a .php extension into URLs without it, making the file extension invisible to users. Even if the website is developed with PHP, the users will experience a static webpage-like display, which improves website security and aesthetics.

How to Implement Pseudo-static PHP Extension Hiding

Apache Server Configuration

In an Apache server, pseudo-static PHP extension hiding can be implemented by configuring the .htaccess file. First, ensure that the rewrite module is enabled on the server. Then, create or modify the .htaccess file in the website's root directory with the following code:

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

This code rewrites all requests without an extension to include the .php extension. For example, when a user visits http://example.com/about, it will be rewritten to http://example.com/about.php.

Nginx Server Configuration

In the Nginx server, the same functionality can be achieved by modifying the nginx.conf configuration file. Simply add the following code in the location block:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

This configuration will redirect all requests to index.php while preserving the original query string. This way, when a user visits http://example.com/about, they will be redirected to http://example.com/index.php?/about, thus achieving the pseudo-static effect.

Code Example for Pseudo-static PHP Extension Hiding

To demonstrate the application of pseudo-static PHP extension hiding more clearly, here is a simple PHP code example:

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home';

if($page === 'home') {
    echo 'Welcome to the homepage!';
} elseif($page === 'about') {
    echo 'This is the About Us page.';
} elseif($page === 'contact') {
    echo 'Please contact us.';
} else {
    echo 'Page not found.';
}
?>

With the above code, developers can display different content based on the page parameter. When combined with pseudo-static PHP extension hiding configuration, if a user visits http://example.com/about, they will directly see the about page content without exposing the PHP file extension.

Conclusion

Pseudo-static PHP extension hiding is a commonly used technique to enhance website security and aesthetics. By configuring the server's URL rewriting rules, developers can prevent users from seeing the specific PHP file extension when accessing the website, improving both the user experience and SEO. Whether using Apache or Nginx servers, this functionality can be easily implemented through simple configuration.