homepage Welcome to WebmasterWorld Guest from 107.21.163.227
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Remove file extensions (.htm)
I am using .htaccess but it's not working
widewebway




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

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.

 

flycatcher




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

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

flycatcher




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

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.

widewebway




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

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]

jdMorgan




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

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

widewebway




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

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]

jdMorgan




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

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

Jim

g1smd




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

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.

widewebway




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

@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.

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved