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.

3
  • 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? Commented Dec 3, 2011 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: ashleysark.com/?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 Commented Dec 3, 2011 at 22:44
  • works fine for me if i click a link and remove the .php ending Commented Dec 3, 2011 at 22:45

3 Answers 3

96

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

Sign up to request clarification or add additional context in comments.

11 Comments

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..!
@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.
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
You can replace http://example.com/folder/$1 with http://%{HTTP_HOST}/folder/$1 here.
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
|
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).

2 Comments

Likely why Dan in the comments could get it to work without the extension. Good answer.
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)
9

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.