4

the following code is not working to remove the file extension .php

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

my url not changing from example/about.php to example.com/about

Mukilan R
  • 445
  • 5
  • 17
  • You sure you know how rewriting works? It does _not_ magically change the links inside a web page you send out. It processes _incoming_ requests. If you want to change the address in the browser, then you need the redirect flag `[R]` to make it send a second request. – arkascha Mar 01 '14 at 11:00
  • im new to htaccess rewrite. could you tel me how to rewrite the url by removing the .php extension. – Mukilan R Mar 01 '14 at 11:17

4 Answers4

9

I have found the answer. Its working. the code is as follows to rewrite and redirect the url

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_FILENAME}.php [L]

It rewrites and redirects the URL. Thanks all for ur answers.

Mukilan R
  • 445
  • 5
  • 17
1

This RewriteRule should remove the .php extension from a request. Try placing it before your RewriteConds.

RewriteRule ^(.+)\.php$ /$1 [R]
Koen
  • 545
  • 3
  • 12
1

try this

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
RaGu
  • 723
  • 2
  • 9
  • 29
0

try this

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php

or

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])/?$ $1.php [L]
Boopathi Rajan
  • 1,212
  • 15
  • 38