Forum Moderators: phranque

Message Too Old, No Replies

Redirect extensionless URL's to page with extension

         

mountainsoft

2:40 pm on Oct 1, 2012 (gmt 0)

10+ Year Member



Using a rewrite rule in htaccess, I would like to rewrite (301 redirect) URL's to include extensions for those requests that forget them. For example:

www.example.com/product
or
www.example.com/product/

should both redirect to:

www.example.com/product.htm


However, pages in that folder should be left untouched. For example:

www.example.com/product/order.htm

Thanks!

Anthony

[edited by: incrediBILL at 6:56 pm (utc) on Oct 1, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

2:46 pm on Oct 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's see your code.

There's 20 000 previous threads with examples of various redirects. The participants of this forum "do not write your code for you". They merely help you to "get your own code working".

Post your code here along with the details of what problems you are having.

However, you should change TO extensionless URLs for pages. There are many advantages in doing so.

Redirecting a bare folder URL with slash to a page with extension is going to be problematical. That's not expected bahaviour.

lucy24

4:22 pm on Oct 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you saying that you currently have directories and pages with the same name? That is,

example.com/directory/name/
example.com/directory/name/onefile.html
example.com/directory/name/anotherfile.html
example.com/directory/name/thirdfile.html

and also

example.com/directory/name.html

?

For historical reasons I've got the same setup in one directory-- changing at this point would involve massive redirects and I'd have the googlebot pestering me for years to come-- so instead I've got a generic rule that says

RewriteRule paintings/(\w+)(/(index\.html)?)?$ http://www.example.com/paintings/$1.html [R=301,L]

Yes, technically this would also redirect requests for nonexistent directories, ending up at nonexistent pages. If this ever became a problem I'd have to go back and list the directories by name where now I just have \w+. (One directory already behaves differently and therefore has its own rule. Before this one, of course :)) But in practice it's just for search engines-- and the occasional human-- who extrapolate /directory/ from /directory/filename.html and expect to find an index file in that location. The file they get redirected to really is what they're looking for; it's just got the wrong URL.

The double nesting of parentheses is in case mod_dir executes after mod_rewrite (which I think it normally does) and the request comes in without a trailing slash. Otherwise there would be a double redirect.

aakk9999

5:16 pm on Oct 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would imagine that OP is trying to fix incorrect inbound links. From how I read this post, his site uses URLs with htm extension, but sometimes people link to him without extension or even without extension and with slash at the end instead.

I might be wrong but this is the way I interpret his post (or maybe I understood it this way because this is the only reason you would really want to redirect extensionless --> extension).

mountainsoft

5:26 pm on Oct 1, 2012 (gmt 0)

10+ Year Member



Lucy and aakk9999,

Yes, I am simply trying to redirect links if the visitor forgets the file extension, or tries to navigate into a product folder instead of the main product file.

I'm a relative newbie with htaccess and haven't been able to track down working code in my searches. I will try Lucy's sample, but would prefer to avoid spelling out the domain name if possible as I use the same htaccess locally for testing.

incrediBILL

7:07 pm on Oct 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This simple rule might accomplish what you need.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !\.html$
RewriteRule ^(.*)$ $1.html [L]

Basically if the file requested isn't a file, a directory or an .html file it attempts to rewrite it to a .html file internally, not 301 redirect, but you can make that change easily.

IMO, you would be better off permanently abandoning file extensions all together, wholly unnecessary.