Forum Moderators: phranque

Message Too Old, No Replies

Redirect help

         

kstyles

4:33 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



I need to redirect all requests for

domain/item-2915.html where the 2915 can be any # at all
to domain/item_2915.html where the 2915 is whatever # was requested originally.

And I am completely lost with this one.

g1smd

10:41 pm on Dec 9, 2009 (gmt 0)

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



What code have you already tried?

A single line RewriteRule can likely handle this.

kstyles

1:45 am on Dec 10, 2009 (gmt 0)

10+ Year Member



I don't even know where to start and the item #s are generated dynamically.

Sadly I do not know enough about rewrites and redirects for this one. I can do basic stuff. I get a feeling this requires a wildcard for the dynamic item #s.

jdMorgan

2:34 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are two parts to the code:

1) Mod_rewrite (RewriteRule) or mod_alias (RedirectMatch) for the redirection directive. See the Apache module documentation for your server version.

2) Regular expressions for matching and/or capturing the 'bits and pieces' of the requested URL, and for creating back-references to some or all of these pieces so that they can be "copied" from the old URL to the new URL in your redirect directive. See the regular-expressions tutorial cited in our Forum Charter.

Tips:

You need a strategy to start. This can be described as anywhere from the very-general "replace a hyphen in any requested URL with an underscore" to the very-specific "replace a hyphen following 'item' and preceding a numerical string of four digits (or one or more digits, or a minimum of one and a maximum of five digits), followed by '.html'".

It's important to think through this strategy and work it out very carefully so that you don't waste time coding a bad solution -- or coding one that unexpectedly redirects too many or too few URLs.

Our Apache Forum Charter contains links to several useful resources, explains our focus here, and will help you get the most from this forum.

Our Apache Forum Library contains several useful previous threads, ranging from general introductory-level threads to very-specialized-application threads.

WebmasterWorld has a search form that will allow you to search for previous threads using the keywords that you use to define your strategy.

Links to these resources are located at the top of this page.

As explained in our Charter, we'll be happy to help you get your code working and to answer very-specific questions, but limited volunteer resources preclude the possibility of writing your code for you. And doing so wouldn't really be doing you any favors either, since it's very easy to get into very deep trouble very fast using code that you don't fully understand.

SEO comment:

If this were my site, I'd be redirecting the other way. Hyphens are treated as word separators by search engines, whereas underscores are treated as 'letters.' A search for "this_that" will return primarily pages and URLs which contain exactly "this_that" (that is, including the underscore), and not the phrase "this that" or the words "this" or "that."

By contrast hyphens are treated as word separators, almost identically to a space.

In addition, underscores have an annoying habit of 'hiding' under the link underlines on HTML pages, in some cases making it quite impossible to tell that a link is to "page_name.html" (with an undescore) as opposed to "page name.html" (with a space). For example, hover over this link, then check the status bar of your browser: Can-you_tell which_is which? [test.example.com]

And finally, it's easier to say "dash," "hyphen," or "minus" on the phone, radio, or TV than it is to say "un-der-score."

Jim