Welcome to WebmasterWorld Guest from 54.226.32.234

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Removing file extensions

     

Dexie

4:24 pm on Apr 15, 2010 (gmt 0)

10+ Year Member



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

12:23 am on Apr 16, 2010 (gmt 0)

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



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

7:37 am on Apr 16, 2010 (gmt 0)

10+ Year Member



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

7:39 am on Apr 16, 2010 (gmt 0)

10+ Year Member



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

8:23 am on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

8:32 am on Apr 16, 2010 (gmt 0)

10+ Year Member



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

8:42 am on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

8:45 am on Apr 16, 2010 (gmt 0)

10+ Year Member



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

jdMorgan

4:09 pm on Apr 16, 2010 (gmt 0)

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



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

8:01 am on Jun 23, 2010 (gmt 0)

10+ Year Member



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

3:03 pm on Jun 23, 2010 (gmt 0)

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



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month