Trish Siquian
Trish Siquian

Reputation: 525

How to remove .php extension using htaccess

I have .htaccess file and inside it I have this code:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d

  RewriteRule ^ - [L]

  RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>

I tried to add this which removes the .php extension but, I got 404'd.

RewriteRule ^(.*)$ $1.php

What I want to achieve is to add some code on my htaccess to remove the .php extension without removing the existing code above because I used that for my angular route. Thanks!

Upvotes: 2

Views: 5743

Answers (4)

Trish Siquian
Trish Siquian

Reputation: 525

As @starkeen says, just add the following line into my .htaccess code. And here it is :

 Options +Multiviews

Upvotes: 1

Michael
Michael

Reputation: 405

you can try this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

edit:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Upvotes: 1

Rotimi
Rotimi

Reputation: 4825

Try this. Got this from my project:

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Upvotes: 1

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

This one works for me

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

Upvotes: 0

Related Questions