Matt
Matt

Reputation: 35231

How to remove .php extension from URL on server and on localhost with .htaccess file?

Like the title says I need to remove the .php from a URL with a .htaccess RewriteRule.

https://example.com/about.php

should be

https://example.com/about

AND

localhost:8080/about.php

should be

localhost:8080/about

Here is my current .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

Upvotes: 0

Views: 2370

Answers (2)

Abolfazl Ghaemi
Abolfazl Ghaemi

Reputation: 454

You can add this to your .htaccess file, it will remove .php extension from the URL:

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

Upvotes: 0

anubhava
anubhava

Reputation: 785186

You may use these rules in your site root .htaccess:

RewriteEngine On

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

## To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

Related Questions