Remove .HTML and .PHP Extensions with .Htaccess

Remove .php extension with .htaccess

Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.

Re 1) Change the .html to .php

Re a.) Yup, that's possible, just add #tab to the URL.

Re b.) That's possible using QSA (Query String Append), see below.

This should also work in a sub-directory path:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

Remove .html and .php extensions with .htaccess

Yes, I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.

Here is the .htaccess code snippet that will help you:

# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php

# End of Apache Rewrite Rules
</IfModule>

I want to stress some important things here for everybody's reference:

  • This code snippet doesn't remove entry scripts from url (such as
    index.php used by many PHP frameworks)
  • It only removes .php extension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace php with other extension.
  • Don't forget to also remove extension from anchors (links) href.

htaccess - remove .php extension from url

With your shown samples, please try following htaccess rules file.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^ %1? [R=301,L]

##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]

Remove .php and .html extensions AND have directories with same name?

You could be able to give a priority to file when it request comes without / with same name with directory as well as removing both php & html and determine which one to check first like this :

DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

So , by the code above , extensions will be removed then will check first if there is php file match this , then html and finally if there is a directory .

So , the request for /home only will check first a php that named home if it is exist ok , then directory home but the request /home/about will go directly to about inside home directory.

Clear browser cache then test it , if it is Ok , change 302 to 301 to get permanent redirection

Remove php/html extensions and exclude one specific file from the rules

You can add this rule to skip sendmail.php:

RewriteRule (?:^|/)sendmail\.php$ - [L,NC]

Add this rule just below RewriteEngine on line.

How to remove html and php extensions from URL

Removing Extensions

To remove the .php extension from a PHP file you have to add the following code inside the .htaccess file:

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

If you want to remove the .html extension from a html file

RewriteRule ^([^\.]+)$ $1.html [NC,L]

To remove .html

  RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Updated

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

How to remove php extension form URL .htaccess

Check this modified rule

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)\.(php|html|htm)$ /$1 [R=301,L]

how can I remove php extension using htaccess?

With this code your path will be /blogs:

RewriteEngine On
RewriteBase /omsi_site/

RewriteRule ^blogs$ blogs.html?title=Responsive%20web%20design [L]


Related Topics



Leave a reply



Submit