1

can anyone help me to remove .html or .php extension in URL.

I am trying this below code in my php project, but it is not working. I have tried so many .htaccess codes, but nothing helps me..

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.php

Thanks in advance

2
  • Is the .htaccess file really effective? For example, are you using Apache httpd? Did you enable the rewrite module? Commented Sep 30, 2015 at 5:55
  • 1
    I am running XAMPP and i have created in my htdocs folder, now .htaccess working for all my projects which are in htdocs, any way thank you FelisCatus Commented Sep 30, 2015 at 6:06

1 Answer 1

8

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.. :)

Sign up to request clarification or add additional context in comments.

3 Comments

Is is possible to make it work for both without repeating the code for each extension?
You are genius!!! above 50-100 codes have been replaced, and this is the last one :)
Thanks a lot, gone through multiple questions and answers, neither worked until yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.