1

I was wondering if there was a way to hide the .php file extension with .htaccess.

I've seen quite a few websites do this, for example facebook on most of its pages does not show the .php file extension.

For example I want all my url's to be:

https://mywebsite.com/about

instead of

https://mywebsite.com/about.php

EDIT

Inside my .htaccess file I have the following code already:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Do I just append

RewriteEngine On
RewriteRule ^(.*)$ $1.php

at the end of the file? Or how does it work?

CodeTrooper
  • 1,890
  • 6
  • 32
  • 54

7 Answers7

3

Try this:

This code is currently working in my pc.

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

It will remove extension .php from URL and set remaining URL as it is.

  • Thanks
Chandresh M
  • 3,808
  • 1
  • 24
  • 48
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
2

your have to active the rewrite_mod

and in your .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
CodeIsLife
  • 1,205
  • 8
  • 14
1

While this has been answered a few times already, a quick search will show you results like this that should work:

RewriteEngine  on
RewriteRule ^(.*)$ $1.php
duellsy
  • 8,497
  • 2
  • 36
  • 60
1

add the following to the .htaccess file (make sure you have mod_rewrite enabled)

    RewriteEngine  on
    RewriteRule ^(.*)$ $1.php
josephtikva1
  • 789
  • 5
  • 11
1

Add:

Options Multiviews

and let mod_negotiation take care of this for you.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1
RewriteEngine On
RewriteRule ^(.*)$ $1.php

Second line explanation:

^ Start of line
(  Capture everything enclosed (start)
. Any single character
* Zero or more of any character
) Capture everything enclosed (end)
$ End of line
$1.php Get the captured match and append .php, there's the rewrite!

Voila!

hello => hello.php
home => home.php
user3121056
  • 339
  • 3
  • 12
  • I already have `RewriteEngine on` `RewriteCond %{HTTPS} off` `RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]` Do I just append that code at the end of the line? – CodeTrooper Dec 31 '13 at 02:33
0

by the way ,if you are not familiar with .htaccess syntax,

you can try this htaccess rule generator for generate rules

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57