homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

What allows the .php extension to be left off in the URL?
My test PHP server allows me to access files without using the extension.

 1:03 am on Mar 2, 2010 (gmt 0)

I'm not sure if this belongs here or in the Apache forum.

My local test server processes pages without the use of the .php extension in the URL. What is allowing this?

For example:

file: /some/dir/test.php
url: /some/dir/test

This url will give me the test.php script.

This wouldn't be a problem except that is seems to be skipping my rewriterules.

RewriteEngine on

RewriteRule ^logout/$ index.php?logout=true [QSA,L]
RewriteRule ^login/forgotten$ login.php?forgotten=true [QSA,L]

RewriteRule ^(bikes|locations|contacts|services|users)/new(/([0-9]+))?$ $1/edit.php?id=new&relID=$3 [QSA,L]
RewriteRule ^(bikes|locations|contacts|services|users)/([0-9]+)$ $1/info.php?id=$2 [QSA,L]
RewriteRule ^(bikes|locations|contacts|services|users)/edit/([0-9]+)$ $1/edit.php?id=$2 [QSA,L]
RewriteRule ^(bikes|locations|contacts|services|users)/delete/([0-9]+)$ $1/info.php?id=$2&del=true [QSA,L]

I'm trying to use the url: /bikes/edit/84 to rewrite to /bikes/edit.php?id=84

But since the file edit.php exists in the bike directory, the url seems to be loading the edit.php directly, without any query parameters (why didn't it care the "/84" was on there?).

Bottom line, I'm not getting my $_GET['id'], like I want, and I'm not sure why. This doesn't happen on my development server, which leads me to believe it's a local (PHP?) configuration. But I've never heard of it.

Sorry if this is straight forward. I wasn't sure how to search for it.



 7:41 am on Mar 2, 2010 (gmt 0)

You can leave the extension out using a simple htacess rule

something like:

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

Are you sure that there is no such thing in your htaccess (especialy in root level)?


 4:06 pm on Mar 2, 2010 (gmt 0)

Thank you for your reply, but I don't want to leave the extension out. It seems that the server is loading my edit.php script, without running the .htaccess at all.

I know the .htaccess is working otherwise, because the /logout/ rule works (and others that don't happen to match us with a file path).

URL: /bikes/edit/84

The server loads /bikes/edit.php, but my $_GET array is empty. If I change edit.php to edit2.php, then the $_GET array has "id".

Why is that?


 5:02 pm on Mar 2, 2010 (gmt 0)

This effect can be caused by either the MultiViews option (content-negotiation) being enabled, or if AcceptPathInfo is "On". In either case, these settings take precedence over mod_rewrite.

See Apache core "Options" directive and mod_negotiation, and if you're on Apache 2.x, the "AcceptPathInfo" directive as well.



 5:40 pm on Mar 2, 2010 (gmt 0)

That you so much. I was having a hard time articulating what I needed to say.

Solution: In my httpd.conf, I found:

<Directory "/Users/mdeclair/Sites">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all

Removed the "MultiViews" and restarted my server.


 7:34 pm on Mar 3, 2010 (gmt 0)

Remove "Options Indexes" as well, unless you wish to allow visitors to display the file directory of subfolders in which no "index.php" or "index.html" file exists...

The code above to "remove extensions" should not be used without several additional rewriteconds, as otherwise it will invoke four calls to the OS to do file- and directory-exists checks. This is a very inefficient function, and may require a physical disk read. Therefore, the 'target' path of the rule should be excluded by a rewritecond, and another rewritecond should be considered which excludes filetypes which are not to be handled by the script -- such as image, CSS, Javascript, document and media files. This again avoids unnecessary exists checks.

Many major CMS packages omit this improvement, and can be sped up simply by adding these exclusions -- sometimes noticeably so.



 7:55 pm on Mar 3, 2010 (gmt 0)

Which "code above" are you referring to? My first post, omoutop's, or my 5:40pm post?

Regarding the "Options Indexes", I'm going to leave the settings as default as I can, as I'm trying to mirror on other environment. Thanks for the heads up, but I tend not to leave directories un-index.php'ed.


 11:42 pm on Mar 3, 2010 (gmt 0)

I thought I would attach another issue that I am currently having. I have the following .htaccess:

DefaultType application/x-httpd-php

AddHandler x-httpd-php .php4
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^([^.]+)\.php$ h**p://www.mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ h**p://www.mysite.com/$1 [R=permanent,L]

With jdMorgans useful advice, I have had success with the above. One issue that I am currently having is that I would like to be able to have all my index pages from my sub directories drop the /index part. I have seen other solutions that would work, but not combined with my code above, which drops the .php from my files. Any help is appreciated.


 12:05 am on Mar 4, 2010 (gmt 0)

I understand that you have a similar issue, but it is different from mine. Please open a new thread. Having different problems/solutions in one thread can be confusing for anyone using this post as a reference.


 2:17 am on Mar 4, 2010 (gmt 0)

The "code above" :

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

This is a very inefficient method, especially in a .htaccess context, where its performance can be doubled simply by adding one line:

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

This prevents the file- and directory-exists checks from being repeated when mod_rewrite is re-started after this rule itself is applied (which always occurs in a .htaccess context).

To further improve it, you can exclude *any* requested URL-path that already has a file extension, since it's very unlikely you'd want to rewrite that URL to add a .php extension:

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

The whole point is to avoid file- and directory-exists checks unless they're absolutely required.

The same applies to checking the %{REMOTE_HOST}, which invokes an outgoing reverse-DNS lookup from your server, hanging your current visitor's request until a reply is received from the DNS server. This should be avoided as well.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved