homepage Welcome to WebmasterWorld Guest from 54.167.177.180
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

    
Removing file extensions
Dexie

5+ Year Member



 
Msg#: 4116416 posted 4:24 pm on Apr 15, 2010 (gmt 0)

Been trying to code for all possible file extensions, but am getting stuck - have tried the code below, but no luck. Can anyone lend a hand please ?

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.(php|html|css|jpg|gif|png) -f
RewriteRule ^([^/]+)/$ $1.html

Any help much appreciated.

Dexie

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4116416 posted 12:23 am on Apr 16, 2010 (gmt 0)

We'd be happy to help, but you'll need to tell us what you are trying to do here, as the "logic" of this code is inconsistent, and the syntax isn't right. One way to save a lot of time in coding and a lot of trouble in testing is to be sure you have a solid 'plan' of how you intend the code to work. Once a good plan is in place, the most-time consuming parts of coding will be typing and proof-reading...

Jim

Dexie

5+ Year Member



 
Msg#: 4116416 posted 7:37 am on Apr 16, 2010 (gmt 0)

Thanks Jim, just trying to clean up the url's, so that no .php's, htm's, .html's, .jpg's etc, that don't show at the end.

Also tried :

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/$ /$1.jpg [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.htm [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.html [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]

But that didn't work either ;-(

Any help much appreciated.

Dexie

Dexie

5+ Year Member



 
Msg#: 4116416 posted 7:39 am on Apr 16, 2010 (gmt 0)

Going to try that again, but wording it properly this time ;-)

Thanks Jim, just trying to clean up the url's, so that .php's, htm's, .html's, .jpg's etc, never show at the end of the url's.

Also tried this in the .htaccess :

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/$ /$1.jpg [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.htm [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.html [L]

RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]

But that didn't work either ;-(

Any help much appreciated.

Dexie

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4116416 posted 8:23 am on Apr 16, 2010 (gmt 0)

When I ask for
example.com/something how will your site "know" whether that's supposed to be an HTML page, an image or the CSS file?

Your last example code maps ALL requests to
<something>.jpg as the first rule matches ALL requests. The URL needs to contain a 'clue' as to what is wanted.
Dexie

5+ Year Member



 
Msg#: 4116416 posted 8:32 am on Apr 16, 2010 (gmt 0)

Thanks for the input - it's appreciated.

When someone clicks on a link to, for example :
example.com/something.php

It needs to show in the address bar, as :
example.com/something

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4116416 posted 8:42 am on Apr 16, 2010 (gmt 0)

It can't possibly do that. Once the user has clicked the link, the URL is already in the browser URL bar.

URLs are defined by links. If the user is supposed to 'see'
example.com/something in the URL bar of the browser, then that is what needs to go in the HREF of the link on that page of your site.

Once that request arrives at your server, it is the job of
.htaccess to connect that request to the actual file.

It's not a good idea for images, CSS and JS files to have URLs without the extension.

However, it is very easy to have page URLs without extensions and to connect those requests to .html or .php files. Use a rewrite for that.

Once the rewrite works, add a redirect such that if the user continues to request a URL with .php on the end (by following links cached by searchengines, or from bookmarks) they are redirected to URL without the .php on the end.

A rewrite maps a URL request to an internal filepath. A redirect accepts URL requests and directs the browser to make a new request for the new URL.

[edited by: g1smd at 8:46 am (utc) on Apr 16, 2010]

Dexie

5+ Year Member



 
Msg#: 4116416 posted 8:45 am on Apr 16, 2010 (gmt 0)

Thanks, just trying to make the url's look a lot 'cleaner' without all the .php, .html, .htm extensions at the end.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4116416 posted 4:09 pm on Apr 16, 2010 (gmt 0)

Yes, you need to edit the links on your pages, or edit the script that generates those links.
That's step one.

Step two is to write a rule that internally rewrites --"connects"-- those new extensionless URLs, now being requested as a result of your new on-page links, to the correct filepath-with-extension in your server's filesystem.

The third -and optional- step is to detect direct client requests for old URLs that still have extensions, and redirect them to your new extensionless URLs.

If it isn't clear, your old URLs used to be more-or-less the same thing as your filenames. Now they are different. You define the new extensionless URLs on your Web pages, and add mod_rewrite code to re-establish the association between those URLs and your existing filenames.

The optional third step only speeds up the re-indexing of your new URLs and the elimination of your old URLs from search results listings. It is not required.

You can only 'go extensionless' with a very few types of URLs. Trying to allow too many filetypes will bog down your server due to excessive 'file-exists' checking (each of these checks may result in additional reads of the physical disk, which are slow compared to everything else).

Here's an example for .php and .html only:

# Externally redirect direct client requests for URLs ending in .php to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect direct client requests for URLs ending in .html to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless URLs to add ".php" if a corresponding php file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
#
# Internally rewrite extensionless URLs to add ".html" if a corresponding html file exists
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.html [L]

Notice that the order of the third and fourth rules establishes a priority of .php files over .html files; If both types of file exist, the php file will be served, and the html file will be inaccessible via HTTP.

The order of the first two rules does not matter, because they are mutually exclusive. You might want to put the rule for the most-frequently-requested filetype first, for a small efficiency improvement.

Note also that the first two rules can be combined, but only if you never rewrite or redirect .html URLs to .php URLs or filepaths, or vice-versa -- now or later.

Jim

Dexie

5+ Year Member



 
Msg#: 4116416 posted 8:01 am on Jun 23, 2010 (gmt 0)

Apologies Jim, for some reason I wasn't getting the notifications of replies, but am now.

Many, many thanks for the detailed response - it's worked perfectly.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4116416 posted 3:03 pm on Jun 23, 2010 (gmt 0)

Also, the only "things" that need to -or should- use extensionless URLs are "pages." There is no need or benefit to making images and other 'included objects' such as CSS or .js files extensionless, and doing so just adds more code and more disk checks, and makes your site (possibly Much) slower.

Jim

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