0

Possible Duplicate:
remove .php extension with .htaccess

I have

http://www.example.com/test/categoryform.php

In my .htaccess file, how do I rewrite that to display as:

http://www.example.com/test/categoryform/
Community
  • 1
  • 1
Michael Crothers
  • 191
  • 3
  • 3
  • 10
  • 1
    Welcome to SO. Before asking a question feel invited to use the search and do some research on your own first. It's not that we do not like your question, it is just that it has been asked and answered before, so there is no need to ask it again. You might see some downvotes because it is asked very often, so you did some common mistake which triggers some moods on this site. But anyway, welcome to SO. – hakre Aug 27 '12 at 08:51
  • I did research a little but my issue was being caused by something else (needed RewriteBase, which almost no-one mentions) – Michael Crothers Aug 27 '12 at 09:15
  • Sure, RewriteBase is well explained in the HTTPD manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html - which as you can see is even on the same page with RewriteCond and RewriteRule as well as RewriteEngine. – hakre Aug 27 '12 at 09:51

3 Answers3

5

try this code out

RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename

and How to hide .php extension in .htaccess question is also usefull

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

Easy question asked a thousand times

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

Source: Removing file extension via .htaccess

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • 2
    I think it's **over 9000**... you should point to a duplicate in comments instead of answering. – Dejan Marjanović Aug 27 '12 at 08:53
  • I was aware of that. Problem was, none of the suggestions would work for me. I kept getting a 404 error. But I just found out that it was because I needed to declare RewriteBase as `RewriteBase /test/. Thanks anyway. – Michael Crothers Aug 27 '12 at 09:13
  • 1
    @MichaelGeorgeCrothers: Just do not copy and paste code from other sources whithout understanding what it does. At least if you run into a problem, start to understand it first. – hakre Aug 27 '12 at 09:53
  • I flagged it (with the appropriate duplicate) and answered the question. I didn't know that it was a 'convention' to do it that way and cannot find it in the faq, thought now that I think about it it's not the first time I see it. – Francisco Presencia Aug 27 '12 at 15:07
  • PS, I edit it for future references, check that this is the way that you have it. – Francisco Presencia Sep 01 '12 at 17:10
  • @hakra: I can now understand the RewriteCond code and I thank the community for helping me understand that however I feel that RewriteBase is in a different context compared to RewriteCond at least for me and that in some cases, it is necessary for this kind of procedure to work. – Michael Crothers Sep 06 '12 at 12:44
1

In order to remove the trailing slash, you need to match it out in a condition

RewriteEngine On
# make sure it's not a directory or a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# match out the request URI without the trailing slash
RewriteCond %{REQUEST_URI} ^/([^/]+?)/?$
# and see if it exists
RewriteCond %{DOCUMENT_ROOT}/%1.php -f

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

But in order to get it to display (as in, it show up in the browser's address bar, you need:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+?)\.php
RewriteRule ^(.+?)\.php$ /$1/ [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220