/    Sign up×
Community /Pin to ProfileBookmark

hide .php extension using .htaccess

hi all,
i am trying to hide the “.php” extension in mywebsite. here i can do this only by changing “.php” to “.htm” or “.html”. as
[url]http://localhost/shopping/cart/checkout.php[/url]

to
[url]http://localhost/shopping/cart/checkout.htm[/url]

but i dont want to do like this instead, i want the following

[url]http://localhost/shopping/cart/checkout[/url]

can anybody please help me to do this using .htaccess

thanks in advance

to post a comment
PHP

43 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyFeb 12.2007 — [url=http://www.ryanbrill.com/archives/cruft-free-urls/]Cruft-free URLs[/url]
Copy linkTweet thisAlerts:
@HomoErectusFeb 12.2007 — Recommending mod_rewrite for this is madness. Having a cruft-free URL is a feature of mod_negotiation and does not require any rewrite rules.
Copy linkTweet thisAlerts:
@bathurst_guyFeb 12.2007 — Have you got an example I can look at?
Copy linkTweet thisAlerts:
@bokehFeb 12.2007 — Have you got an example I can look at?[/QUOTE]That is how the links in my signature don't have extensions. All that was needed was to remove the "#" in front of load module [I]mod_negotiation[/I] in my .httpd.conf file.
Copy linkTweet thisAlerts:
@jenyauthorFeb 13.2007 — can anybody please give an example for this?
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — can anybody please give an example for this?[/QUOTE]
Is your server running [I]mod_negotiation[/I]? Check with [I]phpinfo()[/I].
Copy linkTweet thisAlerts:
@MrCoderFeb 13.2007 — In your .htaccess
[CODE]
<files checkout>
ForceType application/x-httpd-php
</files>
[/CODE]
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — In your .htaccess
[CODE]
<files checkout>
ForceType application/x-httpd-php
</files>
[/CODE]
[/QUOTE]
That's certainly better than using mod_rewrite.
Copy linkTweet thisAlerts:
@jenyauthorFeb 13.2007 — i included the code


<files checkout>

ForceType application/x-httpd-php

</files>

but even this gives me http://localhost/shopping/cart/checkout.php

help me please...
Copy linkTweet thisAlerts:
@MrCoderFeb 13.2007 — i included the code


<files checkout>

ForceType application/x-httpd-php

</files>

but even this gives me http://localhost/shopping/cart/checkout.php

help me please...[/QUOTE]


http://localhost/shopping/cart/checkout.php

Should be a php file without the .php extenstion.. Like this..

http://localhost/shopping/cart/checkout
Copy linkTweet thisAlerts:
@jenyauthorFeb 13.2007 — i saved the checkout file as checkout.php and when i run http://localhost/shopping/cart/checkout error gives that

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost

02/13/07 16:03:39

Apache/2.2.0 (Win32) DAV/2 mod_ssl/2.2.0 OpenSSL/0.9.8a mod_autoindex_color PHP/5.1.1

it is treating checkout file as a directory.
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — At the moment your file is named [I]checkout.php[/I]. Rename that file to [I]checkout[/I] (no extention), then run the code supplied by MrCoder.
Copy linkTweet thisAlerts:
@bathurst_guyFeb 13.2007 — So does that then run every file through the PHP processor?
Copy linkTweet thisAlerts:
@MrCoderFeb 13.2007 — <files [B]FileName[/B]>

ForceType application/x-httpd-php

</files>

It will force the file with the matching filename to run via PHP.

For example if I had a file called cart.php and renamed it to cart.html I could still run it as PHP by using..

[CODE]<files cart.html>
ForceType application/x-httpd-php
</files>
[/CODE]
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — <files [B]FileName[/B]>[/QUOTE]Can that take a pattern as the argument?
Copy linkTweet thisAlerts:
@MrCoderFeb 13.2007 — Can that take a pattern as the argument?[/QUOTE]

I think your looking for <FilesMatch>?
Copy linkTweet thisAlerts:
@decibelFeb 13.2007 — While these things work and all, switching to mod_rewrite, and instanciating classes for every page, then displaying each class using Smarty, is a MORE EFFICIENT way to write PHP... I would never switch back... Ive seen the light... although I realize you are not asking for this advice, If you knew what I know about this technique, you would could say " Why would I ever create chechout.php in the first place"...
Copy linkTweet thisAlerts:
@theuedimasterFeb 13.2007 — Just curious... why would one want to do this anyways?
Copy linkTweet thisAlerts:
@decibelFeb 13.2007 — using a SWITCH to load page classes (that extend parent page classes, or page groups) can make somre really clean urls,,hides the file extension, and become a far more powerfull OO website

some sample urls i use



[code=php]www.mysite.com/store
www.mysite.com/store/checkout
www.mysite.com/fernando
www.mysite.com/users/jim/profile/edit[/code]



these folders dont exist on the server, but a switch() dispatcher loads the class that proceesses your logic code, then sends it to smarty, which is a seperate html file, completely php free

It completely seperates login code from display code, not just css from php

PHP is awesome, but PHP with Smarty (or similar template processing) is TWICE as efficient as embedding html in php, read about smarty here http://smarty.php.net/manual/en/what.is.smarty.php
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — Just curious... why would one want to do this anyways?[/QUOTE]Because it is a W3C recommendation!

[B]Choose URIs wisely[/B] - "[I]Use format negotiation to hide file extensions in URIs, giving you enough flexibility to change the underlying technology without too much pain.[/I]"Smarty, is a MORE EFFICIENT way to write PHP[/QUOTE]The purpose of Smarty is to separate application code from presentation. Doing such a thing adds another layer between the PHP script and the output stage which certainly does NOT increase efficiency.
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — I think your looking for <FilesMatch>?[/QUOTE]
What I'm getting at is making a dynamic rule. For example send all files that don't have and extension to PHP.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — The purpose of Smarty is to separate application code from presentation. Doing such a thing adds another layer between the PHP script and the output stage which certainly does NOT increase efficiency.[/QUOTE]


The reason why a website would strongly benefit from this is because you can build a website that is extremely powerful, performing very extensive php processing, and then, if you just want to change the way your website looks, i mean html layout, not just css, its extremely simple, and doesn't require php programmer skills to do it, anybody who knows html can do it.

I just built a website, that is extremely powerful, and simply skinable. Take a look at a myspace page that is for a brand or advertisement, same myspace functionality, with a completely different look and feel. They sell these pages for hundreds of thousands of dollars... and all it takes is a junior programmer, who has never seen php, to change the template files, he would never see the application files and code.

Template style of coding is the basis of MVC style coding, and if MVC isn't more efficient, then I better quit my job as a programmer and find another occupation

If creating and seperating a logic layer and a presentation layer is not more efficient , then I will be the next president of the United States... but I guess some old school programmers would disagree.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — If you want to know the purpose of Smarty, read this:

http://en.wikipedia.org/wiki/Model-view-controller
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — I just built a website, that is extremely powerful, and simply skinable. Take a look at a myspace page that is for a brand or advertisement, same myspace functionality, with a completely different look and feel. They sell these pages for hundreds of thousands of dollars... and all it takes is a junior programmer, who has never seen php, to change the template files, he would never see the application files and code.[/QUOTE]I don't see how your posts are related to what the OP has asked.If creating and seperating a logic layer and a presentation layer is not more efficient , then I will be the next president of the United States... but I guess some old school programmers would disagree.[/QUOTE]Looks like you haven't read the documentation on Smarty's website.
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Smarty may be fine for the junior programmers to which you are referring. It may, in some special cases where general PHP and HTML knowledge is lacking, amount to a more efficient use of said junior programmers.

The efficiency to which Bokeh is referring is not based on the efficient use of junior programmers without PHP programming knowledge but on the efficient use of a server's resources - which by definition are always scarce. Seen from this perspective, Smarty is anything but an efficient use of those resources.

While Smarty adequately separates logic from presentation, any junior programmer worth his salt can do the same. When one refers to the separation of logic and presentation, "complying" with this precept does not mean having separate files, as required by Smarty. It can be accomplished in a single script, as long as there is a clear demarcation between both.

I have used Smarty in the past - but not recently, I must admit. I have had no urge to revisit it, however, because the bad taste it left then is still with me. Unless Smarty actually helps me produce more efficiently and unless Smarty produces efficient results, Smarty is not for me.

In general, you cannot consider a pseudo-interpreter running under an interpreter as efficient. This is almost akin to having an electric car using a diesel generator to power itself. We all know how efficient that would be.

So, for the moment, I'm voting for Bokeh for President of Costa Blanca. And against Smarty...

If you MUST use a templating system, you should consider this one: http://sourceforge.net/projects/yats/. It will run circles around Smarty.
Copy linkTweet thisAlerts:
@jenyauthorFeb 14.2007 — i got it worked.. if i want all files to work in this way then shall i do like this

<files cart, checkout,index>

ForceType application/x-httpd-php

</files>

thanks for all of yur responses.
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — i got it worked.. if i want all files to work in this way then shall i do like this

<files cart, checkout,index>

ForceType application/x-httpd-php

</files>

thanks for all of yur responses.[/QUOTE]


Doing the following would be easier because it doesn't need to contain the filenames:[CODE]<FilesMatch "^[^.]+$">
ForceType application/x-httpd-php
</FilesMatch>[/CODE]
Copy linkTweet thisAlerts:
@jenyauthorFeb 14.2007 — thanks for your reply bokeh. is smarty included in php
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — is smarty included in php[/QUOTE]No!
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — I know change isn't easy for some, but all I can say is this, i you believe PUTING HTML AND PHP IN THE SAME FILE is an acceptable way to write code for websites, then please continue to do that. You are giving me job security, and you have me rolling on the floor.

If you've ever worked for a major website company, I mean publicly traded, then you'd probrably know that they have a department of integrators, and a department of programmers... what im saying is that smarty is so easy for an integrator, he does not need to know any php. Then theres the programmers, they write the php, logic, complex stuff... and never have to write any html... it doesn't take a genious to understand why this is important and efficient for a large company.

I guess some of you guys dont understand smartys compiling and caching abilities, just like JSP, you cache compiled templates, leaving server resources open to do other things. templates that you don't want cached, you can specify.

If I allowed some of you to get a peek at the framework I use, which was thankfully shown to me 6 months ago, you would start drooling at the way it handles smarty, but then, perhaps its a little ahead of you OLD SCHOOL guys time.

If you want to write to most powerful PHP websites possible, learn about smarty. the samples on the smarty website dont show you half the power smarty has when integrated with a true OO framework.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — A quick sample of how mod_rewrite and php work together, some things to note:

every page is class, an object... you will only have index.html in your root

optimized for PHP 5,

uses autoload to find object files, you dont need to include them when you call them

after myPage object processes php, sends to Smarty with built in smarty object... no need to instanciate Smarty ever, its always there, all you need to set your template variables with one simple statement

this is for a site like myspace, handles social networking, profiles, comments, messaging, and has a store

[code=php]
//What page are we looking for?
$myURL = $_SERVER['REQUEST_URI'];

$cleanURL = explode("?", $myURL);

$requestURL = $cleanURL[0];
$redirectURL = $myURL;

$_pageData = new _pageData();

$_pageData->_pageURL = $myURL;

//Look for group
$myURLPieces = split("/", strtolower($requestURL));


//Look for homepage

if(!$myURLPieces[1] || $myURLPieces[1] == 'index.php' ) {

$pageGroup = 'homePage';

}else{
$pageGroup = $myURLPieces[1];
$_pageData->_pageGroup = $pageGroup;
}

//is there a subgroup?
if($myURLPieces[2] != ""){
$subGroup = $myURLPieces[2];
$_pageData->_subGroup = $subGroup;
}

//is there a mode?
if($myURLPieces[3] != ""){
$pageMode = $myURLPieces[3];
$_pageData->_pageMode = $pageMode;
}

//is there a command?
if($myURLPieces[4] != ""){
$command = $myURLPieces[4];
$_pageData->_command = $command;

}

//is there a subCommand?
if($myURLPieces[5] != ""){
$subCommand = $myURLPieces[5];
$_pageData->_subCommand = $subCommand;
}


// Page Object Dispatcher
switch($pageGroup) {

case "homePage":

$myPage = new homePage;

break;

case "register":

$myPage = new registerPage;

break;

case "login":

$myPage = new Page;

break;

case "signout":

$myPage = new Page;

break;

case "forgotpassword":

$myPage = new forgotPasswordPage;

break;

case "browse":
case "search":

$myPage = new searchPage;

break;

// MEMBERS //
case "member":
case "users":

switch($pageMode) {

case "validate":

$myPage = new userValidate;

break;

case "settings":

$myPage = new userAccountSettings;

break;


case "resetpassword":

$myPage = new userResetPassword;

break;

case "gallery":

$myPage = new userGallery;

break;

case "blog":

$myPage = new userBlog;

break;

// SOCIAL NETWORKING
case "profile":

$myPage = new userProfileSettings;

break;

case "comments":

$myPage = new userComments;

break;

case "messages":

$myPage = new userMessages;

break;

case "add":
case "friends":
case "invite":
case "block":

$myPage = new userNetworking;

break;

// We either don't have a pageMode, or one
// we don't know what to do with yet, most likely a users home/profile.
default:

// We're looking for a userpage,
// so the subGroup should be the username
$myPage = new userPage;
$myPage->setSubGroup($subGroup);
$myPage->_pageMode = $pageMode;

break;
}

break;


// STORE
case "store":

switch($subGroup) {

case "item":

$myPage = new storeProduct;

break;

case "catalog":

$myPage = new storeCatalog;

break;

case "cart":

$myPage = new storeCart;

break;

case "checkout":

$myPage = new storeCheckOut;

break;

case "orders":

$myPage = new storeOrders;

break;

default:

$myPage = new storePage;

break;

}

break;

case "contact":

$myPage = new contactPage;

break;

case "about":

$myPage = new aboutPage;

break;

// ADMIN SECTION //
case "admin":

switch($subGroup){

// ADMIN STORE
case "store":

switch($pageMode){

case "items":

$myPage = new adminStoreItems;

break;

case "invoices":

$myPage = new adminStoreInvoices;

break;

case "categories":

$myPage = new adminStoreCategories;

break;

default:

$myPage = new adminPage;

break;

}

break;

// CONTENT
case "content":

$myPage = new adminContent;

break;

case "maintenance":

$myPage = new adminMaintenance;

break;

case "users":

$myPage = new adminUsers;

break;

default:

$myPage = new adminPage;

break;
}

break;



default:

//Unknown page!
$myPage = new Page;

//Error will kill page progress
$myError = new error;
$msg = "Page does not exist";
$myError->create($msg, $GLOBALS['requestURL'], '404', '');

break;

}

[/code]
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Funny guy ?
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — If you've ever worked for a major website company [...] it doesn't take a genious to understand why this is important and efficient for a large company.[/QUOTE]So basically what you are saying Smarty is great for companies that want to have a low-paid, unskilled workforce.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — Do you know what the difference between an integrator and a programmer is? Did you know they are two completely different positions at a large scale web development company? Apparently not, could someone please explain it to him.

Oh, perhaps i confused you? I guess it might take a genious to understand... im sorry for confusing you.
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — Do you know what the difference between an integrator and a programmer is? Did you know they are two completely different positions at a large scale web development company? Apparently not, could someone please explain it to him.

Oh, perhaps i confused you? I guess it might take a genious to understand... im sorry for confusing you.[/QUOTE]
Argumentum ad hominem consists of replying to an argument by attacking or appealing to the person making the argument, rather than by addressing the substance of the argument. It is most commonly used to refer specifically to the ad hominem abusive, or argumentum ad personam, which consists of criticizing or personally attacking an argument's proponent in an attempt to discredit that argument.

Ad hominem abusive usually and most notoriously involves insulting or belittling one's opponent, but can also involve pointing out factual but ostensibly damning character flaws or actions which are irrelevant to the opponent's argument. This tactic is logically fallacious because insults and even true negative facts about the opponent's personal character have nothing to do with the logical merits of the opponent's arguments or assertions. This tactic is frequently employed as a propaganda tool among politicians who are attempting to influence the voter base in their favor through an appeal to emotion rather than by logical means, especially when their own position is logically weaker than their opponent's.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — answer the question that you sidestepped... do you comprehend the difference between an integrator and a programmer?
Copy linkTweet thisAlerts:
@bokehFeb 14.2007 — Those are just titles given to employees to keep them busy looking down upon and sucking up to one another in a bid to distract them from the enslavement in which they are trapped. Fortunately, I am not a componant of that heirarchy of subordinates.
Copy linkTweet thisAlerts:
@TaschenFeb 14.2007 — Those are just titles given to employees to keep them busy looking down upon and sucking up to one another in a bid to distract them from the enslavement in which they are trapped. Fortunately, I am not a componant of that heirarchy of subordinates.[/QUOTE]

Hehehe... The best retort to one of the daftest, indeed fallacious of arguments I've witnessed for a long time.
Copy linkTweet thisAlerts:
@decibelFeb 14.2007 — sidestep...
Copy linkTweet thisAlerts:
@NightShift58Feb 15.2007 — Bokeh side-stepping? That would be a major first...

I like your sense of humor... Hillarious! ?
Copy linkTweet thisAlerts:
@theuedimasterFeb 15.2007 — Those are just titles given to employees to keep them busy looking down upon and sucking up to one another in a bid to distract them from the enslavement in which they are trapped. Fortunately, I am not a componant of that heirarchy of subordinates.[/QUOTE]

Bokeh, thinking everyone is a genius is false, and you know it. You're disappointed that not everyone is a master web programmer; hell, I'm disappointed too. But the fact is that this is not the case. Sometimes, to make a group of programmers efficient, by making them focus on specific tasks, programs such as smarty can help people collaborate faster. And it is, by definition, that efficiency is where each part of a machine works in it's specific role and does not mess with the others.

On a different note, why can't smarty be efficient code-wise anywas? Let's look at the definition from dictionary.com


performing or functioning in the best possible manner with the least waste of time and effort; having and using requisite knowledge, skill, and industry; competent; capable: a reliable, efficient secretary.
[/quote]


By understanding that a website has two different interactions, one with the programmer and updater, and one with the user, it makes sense that an efficient website would answer to both needs. The code needs to be tight, cohesive, and as free of bloat-logic as possible. At the same time, the code needs to be accessible enough to allow changes with the least waste of time and effort. A program like smarty might just make this happen.

It's naive to think that efficiency of code is only related to how fast it can punch through a cpu. That is only a one-sighted approach to the problem. A code is also efficient based on it's ability to be understood and changed. Now, I can understand that you might classify this is more of a property of mobility, but I think it can also apply to efficiency.
Copy linkTweet thisAlerts:
@NightShift58Feb 15.2007 — Bokeh, thinking everyone is a genius is false...[/quote]Did you mean Bokeh? Or were you referring to: Oh, perhaps i confused you? I guess it might take a genious to understand... im sorry for confusing you.[/quote]I'm losing track of the geniuses...

I understand your point. It's all in the definition of efficiency and we can agree or disagree or lean in one direction more than the other. It would be of great benefit to this forum if all arguments brought forth were done in the same civil manner as you've done.

Earlier, the point was being made that Smarty is the tool of choice for all Fortune 500 companies and that the rest of us haven't a clue. Well, some of us may or may not have a clue - old school or not. But the claim that all larger companies use Smarty is utterly false. The assumption that other posting members in this thread have not worked in "major" projects is, I guarantee you, just as false.

Bokeh's point is that to argue a case is one thing, but to build that case by attempting to diminish your opponent in the process is another. The other poster should build his case on the validity of his arguments and not in belittling a valued* member of this forum. Attacks on his intelligence and knowledge - one which, to the beneift of many, he has demonstrated more than a few times - is perhaps a reflection of sorts. It brings spouse-abusers to mind, who, unable to cope, will resort to violence - verbal and physical. No good. I'm sure you'll agree.

There's room for dissent in a forum. There has to be. And so I wonder if all that noise by decibel - pun intended - was appropriate or even necessary.

(* - my personal view)
Copy linkTweet thisAlerts:
@LeeUFeb 15.2007 — O.k., we're way off the original topic now. Thread closed.
×

Success!

Help @jeny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...