Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: Ocean10000 & incrediBILL & phranque

Remove file extensions (.htm)

I am using .htaccess but it's not working

   
2:49 pm on Jul 20, 2009 (gmt 0)

5+ Year Member



Hi All,

I have a web site as below
http://www.example.com/about.htm
http://www.example.com/contact.htm

but I want to show it like--
http://www.example.com/about
http://www.example.com/contact

I have visited this forum and seen some code like below is using as .htaccess file but it is not working.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(. )\.htm\ HTTP/
rewriterule ^(. )\.htm$ /$1 [R=301,L]

Please give me some suggestion that I have been searching it since the last two days but yet not solved as I don't know so much about this programming.

5:24 pm on Jul 20, 2009 (gmt 0)

5+ Year Member



Hi Widewebway,

Please can you enable rewrite logging and post what you see in the logs. In your httpd.conf file, add the following entries, before your rewriteengine on entry

Options +FollowSymLinks
Options +Indexes

RewriteEngine on
RewriteLog "logs/http-mod_rewrite.log"
RewriteLogLevel 3

thanks

6:21 pm on Jul 20, 2009 (gmt 0)

5+ Year Member



The second example at the following link may be a bit complicated than what you want.

[rankinglabs.com...]

But it does look like it does what you're looking for.

7:11 pm on Jul 20, 2009 (gmt 0)

5+ Year Member



Hi Flycatcher,

Thanks for you reply and give me an advice.

But buddy I have change some codes in my site and it is now working! :D

the code is as below
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
rewriterule ^(([^/]+/)*[^./]+)/?$ /$1.htm [L]

7:54 pm on Jul 20, 2009 (gmt 0)

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



Actually, you might want to use both rules -- plus a third.

Your newest rule above internally rewrites extensionless URL requests to filepaths with the .htm file extension.

The rule you posted in your first post was probably intended to redirect requested URLs with .htm extensions to URLs without file extensions to 'clean up' search engine listings that show URls with .htm extensions, and to recover the traffic and PageRank of the old links.

It may take some further tweaking to work properly on your server, but the whole solution is:

1) Change links on your pages to remove ".htm"
2) Rewrite extensionless URL requests to files with .htm extensions (if they exist)
3) Redirect requests for old .htm URLs to new extensionless URLs (if the .htm file exists)
4) Redirect to remove trailing slashes from extensionless URL requests


RewriteEngine on
RewriteBase /
#
# Redirect direct client request for old URL with .htm extension
# to new extensionless URL if the .htm file exists
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.htm\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.htm$ http://www.example.com/$1 [R=301,L]
#
# Redirect any request for a URL with a trailing slash to extensionless URL
# without a trailing slash unless it is a request for an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless URL request
# to .htm file if the .htm file exists
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.htm [L]

So this is the entire 'system' of rules to get rid of old .htm URLs, to canonicalize your new extensionless URLs by removing trailing slashes, and to rewrite your new extensionless URLs to the correct .htm files.

Note that file- and directory-exists checks should be minimized for best performance, since they require a call to the filesystem (and possibly a read of the physical disk) and can therefore be quite slow and resource-intensive. By using specific RewriteRule patterns (as we do here), making the filecheck RewriteCond last of all RewriteConds when possible, and only checking for what we actually need to know, we minimize the performance impact of these rules. (Note that RewriteConds are only processed if the RewriteRule pattern and all previous RewriteConds have been matched. See Apache mod_rewrite "Processing" documentation for more info.)

Jim

11:02 pm on Jul 20, 2009 (gmt 0)

5+ Year Member



Hi Jim,

Thanks to you for the great explanation.

Jim, as I am using the below code in .htaccess and the site's response is perfect as I wanted so if there is any problem or anything more to do please suggest me. the site is <snip> and the code is

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
rewriterule ^(([^/]+/)*[^./]+)/?$ /$1.htm [L]

[edited by: jdMorgan at 5:04 am (utc) on July 21, 2009]
[edit reason] No URLs, please. See Terms of Serice. [/edit]

5:04 am on Jul 21, 2009 (gmt 0)

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



You might want to ask yourself why I would have bothered to post all that if I thought that nothing could be improved... :)

Jim

9:01 pm on Jul 22, 2009 (gmt 0)

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



This later code has inefficiencies, and several flaws, all of which were addressed by the code Jim originally posted.

This stuff is *not* as simple as your three lines of code hopes.

It really is a whole load of other steps too. Jim's post nails the whole process.

7:43 pm on Jul 23, 2009 (gmt 0)

5+ Year Member



@g1smd, the code is working and thanks Jim, I am using it and understand the matter you mention. Sorry buddy I was so confuse so asked you same question twice.

Any how it is working fine.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month