Forum Moderators: phranque

Message Too Old, No Replies

Can I do a redirect for pages without extensions?

         

mickapoo

3:10 am on Nov 4, 2008 (gmt 0)

10+ Year Member



I'd like to know how (or if) I can create a 301 redirect so that if someone enters a URL without an extension, i.e.:

www.mydomain.com/about (no extension)

can I get those to redirect to an .shtml file?

Thank you, for your help.

jdMorgan

11:15 am on Nov 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's possible.

Is it your intention to just "fix" mis-types, or do you want to use (and link to) extensionless page URLs on your site?

Your answer determines many aspects of the solution, starting with whether the correct solution is an external redirect or an internal rewrite, and how many steps are involved.

Jim

mickapoo

2:00 pm on Nov 4, 2008 (gmt 0)

10+ Year Member



Hi Jim,
Thanks for the response. I am wanting users who do not enter extensions to end up on the .shtml page. Not mistypes, but if they purposely enter something like "www.sitename.com/filename" they should end up on www.sitename.com/filename.shtml

Thank you!

jdMorgan

3:11 pm on Nov 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well the only thing that's tricky about this is that you must check that the URL plus a ".shtml" extension resolves to an existing .shtml file before doing the redirect. So you should precede your RewriteRule with this RewriteCond:

# If requested URL plus .shtml resolves to an existing file
RewriteCond %{REQUEST_FILENAME}.shtml -f

Other than than, it's just a matter of detecting requested URL-paths which contain no periods or a trailing slash and are at least one character in length. A good pattern for use in .htaccess might be "^([^.]*[^./])$"

If that pattern matches and the RewriteCond is true, you'd then 301-redirect to the domain, plus the back-reference, plus ".shtml"

If you have any trouble, post your code for further discussion.

Jim

mickapoo

2:30 pm on Nov 8, 2008 (gmt 0)

10+ Year Member



Thanks Jim!

g1smd

2:38 am on Nov 9, 2008 (gmt 0)

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



What was your final coded solution?