replacing .php ext with .html through .htaccess
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteRule ^(.*)\.html $1.php
edit after pulling white rabbit out of the hat
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
Replace .php with .html for all urls in files directory
Add this to your files/.htaccess
RewriteEngine on
RewriteBase /files/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /files/$1.php [NC,L]
this rewriteRule will redirect all .html
requests to .php
meaning that if you enter www.example.com/files/foo.html
then it will be internally redirected to www.example.com/files/foo.php
and your browser will remain at www.example.com/files/foo.html
htaccess change files extension from .php to .html
Have it this way:
ErrorDocument 404 /404page.php
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteRule ^special-offers.html$ special_offers.php [L,NC]
RewriteRule ^sear_search\.html$ search.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9/-]+)\.html$ offer_detail.php?url=$1 [L,NC,QSA]
Your RewriteBase /
directive is routing it to site root directory instead of current directory.
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 replacephp
with other extension. - Don't forget to also remove extension from anchors (links) href.
removing .html or .PHP URL extension using .htaccess not working
Previously I also faced problem with removing .html or .php extension form URL.
I have a Solution Here
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
put the above code in .htaccess file, above code is for .php files.
for .html files go through the below code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
I just replaced the .php with .html
Hope it helps You.. :)
Related Topics
PHP Include(): File Size & Performance
How to Specify the Parent Query Field from Within a Subquery in MySQL
How to Fix Warning from Date() in PHP"
PHP Check If Time Is Between Two Times Regardless of Date
Simulate File Structure with PHP
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
Doctrine2 Association Mapping with Conditions
How to Create Thumbnails or Preview for Videos
Troubleshooting "The Use Statement with Non-Compound Name ... Has No Effect"
Correct Format for Strings/Numbers Beginning with Zero
Change Woocommerce Default Password Security Level
How to Print Multidimensional Arrays in PHP
How Download Big File Using PHP (Low Memory Usage)