Online Marketing Strategies

How to remove .php, .html, .htm extensions with .htaccess.

2022-09-04 | 801 Print Friendly Version of this pagePrint Get a PDF version of this webpagePDF


How to remove .php, .html, .htm extensions with .htaccess

I found myself in the need to remove the URL extensions from my webpage URLs due to SEO friendly URL I was trying to optimize for, apart from replacing the underscores in my page URL with hyphens, I also needed to remove the URL extensions of PHP to become hidden. From a security perspective, I publish a post on HTTP Security Response Headers For web Security,  it is much better to hide your URL extensions as it makes it raises the interest of hackers to assume your file is located in a separate folder on the server.

I will not be stalling so let's go right into it.

seo web analyst blogger community

 

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 to do this we will be utilizing the .htaccess file located in your server public_html, if yours is hidden then try to unhide it.

Open the .htaccess file via an editor then apply the code below to get the parsing to work effectively

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9-]+/)*[A-Za-z0-9-]+)?$ $1.php

For HTML or other URL extensions, you simply replace the PHP with the page extension eg HTML

There are other codes that function as described above, you can also do a rewrite with  files that are not located in the directory path, if you have a file in PHP you can make it call up as HTML

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]

 

Then we have another that allows you to actually set the rewrite to affect even the directory path of the url, eg domain/demo/webpage.php will become domain/demo/webpage

RewriteEngine on 

RewriteRule ^([^/.]+)$ $1.php [L] 

you can also set expose_php to “Off” in php.ini.

Using WHM, you can edit httpd.conf to set .PHP as the default file type, but you can’t do this in cPanel because any update will reset your configuration.

The downside to this is that PHP errors are usually dumped into an error log that isn’t displayed to the client and can only be accessed server-side. If PHP attempts to “display an error page” as it would locally, the browser would only output a blank page. To resolve that issue you might need to have your custom 404 error page as well and have it redirect to handle the custom error page handler your server will normally display.

# Return server 404 page 404.php to your custom 404

RewriteRule ^ /404 [L,R=301]

# Return 404 if original request is .php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?.php[? ].*$"
RewriteRule .* - [L,R=404]

 

We finally have the masking technique to parse your desired page extension

 

###php masking rule for individual files# ####

RewriteCond %{THE_REQUEST} seo-packages.php

RewriteRule ^ /seo-packages.html [L,R=301]

#Rewrite html to php

RewriteRule ^(.*).html$ $1.php

#####ENDOFRULE###

Adding a trailing slash at the end

I received many requests asking how to add a trailing slash at the end, for example: yoursite.com/page/

Ignore the first snippet and insert the code below. The first four lines deal with the removal of the extension and the following, with the addition of the trailing slash and redirecting.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Link to the HTML or PHP file the same way as shown above. Don’t forget to change the code if you want it applied to an HTML file instead of PHP.

 

How to Remove .php from URL in WordPress, you simply apply any of the suitable codes above and use.

There is also the option of allowing this rule to affect all your subdirectory pages, ie to have even domain/path/webpage.php become domain/path/webpage

you will have to use the following.htaccess code

 

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule ^ /%1 [NC,L,R]
 
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).html [NC]
RewriteRule ^ /%1 [NC,L,R]
 
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

Please Note: I have included that of HTML as well for this write-up purpose, if you have any other extension URLs simply replace them in the code above.


comments powered by Disqus

Ads

custom ads side bar

Author

authors image profile

Olatunji Adetunji

I am a seo web analyst and have a love for anything online marketing. Have been able to perform researches using the built up internet marketing tool; seo web analyst as a case study and will be using the web marketing tool (platform).

Tags

Subscribe RSS

Subscribe with Subscribe with facebook Subscribe with google Subscribe with linkedin Subscribe with TwitterSubscribe with Yahoo   

OR




This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

More Posts From Online Marketing Strategies


Random Blogs




Online Marketing Strategies
carauctions
Forex Trading Strategies
LicensedclearingAgent
Licensedclearing Agent
AuctionCars