40

Possible Duplicate:
Remove .php extension with .htaccess

I'm trying to hide the .php file extension but for some reason can't get it to work. My latest attempt was the following:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/$1.php
</IfModule>

I have tried many different variances of code I have found online but still no luck. The .htaccess file is placed within the root directory.

Community
  • 1
  • 1
adamhuxtable
  • 479
  • 1
  • 6
  • 12
  • Looks fine to me (except that you may like to ignore the trailing slash). Are you sure htaccess is allowed on your host and are you sure that mod_rewrite is supported? – Tom van der Woerdt Dec 03 '11 at 22:44
  • Are you hiding the extension for cosmetic or security reasons? If it's the latter, then there are other ways people can find out what scripting language the site is using. Example: http://www.ashleysark.com/?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 – djdy Dec 03 '11 at 22:44
  • works fine for me if i click a link and remove the .php ending – Dan Dec 03 '11 at 22:45

3 Answers3

95

I've used this:

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]

See also: this question

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 1
    Thank u ryanve.. It was amazing..! – Vinit Kadkol Jun 14 '13 at 09:06
  • 1
    This works well with windows system, but unable to rewrite in my Ubuntu 13.04.. Do u have any solution for this..? Thanx in advance..! – Vinit Kadkol Jul 09 '13 at 10:37
  • @VineetKadkol I've used it on shared Linux hosts. It might need tweaking to work but I'm not sure. Can you discern which rule isn't working? Check for conflicts with other rewrite rules that you have. Experiment with the order of your rules. – ryanve Jul 09 '13 at 15:02
  • Thanks for this answer. but i am not able to make it possible for second segment of url for ex if my url is www.abc.com/xyz/somthing than it fails to load page but it will work fine with www.abc.com/abc – M K Garwa Mar 27 '14 at 12:09
  • @ManojGarwa I think it should work if those rules are in your *root* `.htaccess` *and* there are no conflicting rules. If you need to put the `.htaccess` in a sub directory, see the rules in bottom of [basbasbas.com/thesis/.htaccess](http://basbasbas.com/thesis/source/thesis.20110717/_htaccess) – ryanve Mar 27 '14 at 17:04
  • @ryanve I uploaded this .htaccess file with my domain name in root directory without any conflicting but still i am not able to access php files in a folder of my project – M K Garwa Mar 28 '14 at 11:21
  • 5
    You can replace `http://example.com/folder/$1` with `http://%{HTTP_HOST}/folder/$1` here. – Danny Beckett Jul 06 '14 at 19:51
  • I want to hide .php from my URLs (purely cosmetic) but the folder variable is throwing me off! – Tristanisginger Jul 18 '14 at 00:09
  • 1
    My links get redirected to `http://192.168.1.2/blisscount/user/about` from `http://192.168.1.2/blisscount/user/about.php` but now i get a `404 Error` – ChristopherStrydom Aug 08 '14 at 13:53
  • I replaced `http://example.com/folder/$1` with `/$1`. These rules don't seem to work with subdomains. – skibulk Dec 03 '14 at 23:29
  • This would not work if you're using php in the background to do back-end functionalities – Bao Thai Apr 29 '17 at 06:45
  • other option is this way: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] – simon Jan 28 '20 at 07:03
15

The other option for using PHP scripts sans extension is

Options +MultiViews

Or even just following in the directories .htaccess:

DefaultType application/x-httpd-php

The latter allows having all filenames without extension script being treated as PHP scripts. While MultiViews makes the webserver look for alternatives, when just the basename is provided (there's a performance hit with that however).

mario
  • 144,265
  • 20
  • 237
  • 291
  • Likely why Dan in the comments could get it to work without the extension. Good answer. – typeoneerror Dec 03 '11 at 22:55
  • 1
    First solution worked for me. I've added that to root directory .htaccess, using Wordpress. My script is redirecting (.php extension is hidden and not needed) – mrarm Jul 29 '14 at 19:59
8

1) Are you sure mod_rewrite module is enabled? Check phpinfo()

2) Your above rule assumes the URL starts with "folder". Is this correct? Did you acutally want to have folder in the URL? This would match a URL like:

/folder/thing -> /folder/thing.php

If you actually want

/thing -> /folder/thing.php

You need to drop the folder from the match expression.

I usually use this to route request to page without php (but yours should work which leads me to think that mod_rewrite may not be enabled):

RewriteRule ^([^/\.]+)/?$ $1.php  [L,QSA]

3) Assuming you are declaring your rules in an .htaccess file, does your installation allow for setting Options (AllowOverride) overrides in .htaccess files? Some shared hosts do not.

When the server finds an .htaccess file (as specified by AccessFileName) it needs to know which directives declared in that file can override earlier access information.

typeoneerror
  • 55,990
  • 32
  • 132
  • 223