url with-out file extension .php/.html?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

url with-out file extension .php/.html?

Post by jayson.ph »

Hi All,

i wonder how to make a url with-out file extension .php or html?
example:
instead---> samsung.com/nz/consumer.php
here---> samsung.com/nz/consumer
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: url with-out file extension .php/.html?

Post by requinix »

A very simple change, if you're using Apache, is to enable MultiViews. In a .htaccess

Code: Select all

Options +MultiViews
That will let you omit the extension from any file type that Apache recognizes: at the very least .php, likely images and .css and .js and many others.

If you don't want that much then you need mod_rewrite.

Code: Select all

RewriteEngine on
# not a file
RewriteRule %{REQUEST_FILENAME} !-f
# not a directory
RewriteRule %{REQUEST_FILENAME} !-d
# is a file if you add a .php extension
RewriteRule %{REQUEST_FILENAME}.php -f
# add the extension and stop
RewriteRule $ .php [L]
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: url with-out file extension .php/.html?

Post by Pazuzu156 »

Or you can use regexp and do it that way.

Code: Select all

RewriteEngine On
RewriteBase /

RewriteRule ^([a-zA-Z0-9]+)+(|\/)$ file.php?page=$1
This is how I did mine.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply