user6364857
user6364857

Reputation:

Using .htaccess, how can i change file extension at run time

I want to make a website with PHP, but don't want to show the extension like http://example.com/index.php, i have that index.php file but want to show only index.html, may be .html something else..

I don't know how to do, please help me.

Upvotes: 1

Views: 914

Answers (2)

anubhava
anubhava

Reputation: 786289

You can use these 2 rules in your site root .htaccess:

RewriteEngine on  

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

# To internally rewrite /dir/file.html to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)\.html$ $1.php [L]

Upvotes: 2

Marcel Deglau
Marcel Deglau

Reputation: 51

There is a really easy way with .htaccess:

RewriteEngine on  
RewriteBase /

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L] 

Upvotes: 2

Related Questions