user1362021
user1362021

Reputation: 15

Remove the file extension .php using htaccess file

I want to remove the .php from the url through htaccess file. for example home.php to home I'm using the following rewrite rule in htaccess file.

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

I also want to assign the login as index. How can I change it.

Upvotes: 0

Views: 1834

Answers (2)

Jeff
Jeff

Reputation: 143

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp

To add a trailing slash at the end of the URL (for example: http://www.example.com/about-us/ to go to http://www.example.com/about-us.html)

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

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

Upvotes: 0

anubhava
anubhava

Reputation: 784898

This is the code you can use to hide .php extension:

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 %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NC]

Upvotes: 2

Related Questions