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/
3
  • 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. Commented Aug 27, 2012 at 8:51
  • I did research a little but my issue was being caused by something else (needed RewriteBase, which almost no-one mentions) Commented Aug 27, 2012 at 9:15
  • Sure, RewriteBase is well explained in the HTTPD manual: 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. Commented Aug 27, 2012 at 9:51

3 Answers 3

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

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

Comments

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]

Comments

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

6 Comments

I think it's over 9000... you should point to a duplicate in comments instead of answering.
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.
@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.
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.
PS, I edit it for future references, check that this is the way that you have it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.