3

I'm using Url.Encode within a view and it's replacing spaces with + so instead of:

/production/cats-the-musical I'm getting .../cats+the+musical.

I'm sure this is an easy one, but where do you go to configuring which characters are used for this?

I'll be doing this:

public static string EncodeForSEO(this UrlHelper helper, string unencodedUrl)
{
     return helper.Encode(unencodedUrl.Replace(' ', '-'));
}

Until I get a better answer from you guys.

Edit: Thanks Guffa for pointing out my hasty coding.

3
  • don't forget to take care of special chars Commented Aug 22, 2009 at 18:33
  • These would be taken care of by the call to Encode anyway, wouldn't they? Commented Aug 22, 2009 at 18:34
  • Just a comment in general: Url Path encoding is different from Url Parameter encoding. The "+" characters in the path is not a correct replacement for the space " " character and would cause issues. For SEO as you've done above, replacing " " with "-" is a good technique though. MVC can make it appear that paths and query string parameters are interchangeable, but they are not. Commented Jun 18, 2012 at 22:36

2 Answers 2

4

I want to draw attention to Path versus Query String encoding differences

MVC allows / encourages us to write paths (routes) that can be easier to remember than query strings. e.g. /Products.aspx?id=1 could, in MVC, be /Products/View/1

Building on that, it also encourages, for SEO friendliness, other data that may or may not be necessary like /Products/View/1/Coffee

If the name has space characters, or a necessary parameter is a string containing space characters, and you are including it in the Url path, one of 2 things must happen because a ' ' cannot be left in a Url Path or Query string parameter without being encoded.

  1. You must UrlPathEncode() the string
  2. first you transform the spaces in the string,
    • then call UrlPathEncode() as you may have other characters requiring encoding.

Note: there is a big difference between Url Encoding (meant for query strings) and Url Path Encoding (meant for path portions of Urls)

  • cats the musical -> UrlEncode -> cats+the+musical -- this is not valid in a url path
  • cats the musical -> UrlPathEncode -> cats%20the%20musical

If you're following along; going back to Web Forms vs MVC - /Products.aspx?name=Coffee+Beans would be rewritten as /Products/View/Coffee%20Beans

So that leaves us where OP's question starts. Q: How do you get SEO and human Friendly Urls? Q: Use @Guffas code to replace the " " with "-" in your own code before UrlPathEncoding the rest.

In sites I've worked on, when we have a user-entered value used only for SEO (like a blog title or similar) we go a step further normalizing the string output by collapsing successive spaces into a single "-" e.g.

cats         the     musical
which would otherwise be cats-----the-----musical becomes cats-the-musical

Sign up to request clarification or add additional context in comments.

2 Comments

You no longer need to replace spaces with hyphens. Modern browsers will create pretty urls for you, no more %20soup.
@ChrisMarisic thanks for the update. It appears Chrome and Firefox will now interpret that a user meant to encode the url, and are not as strict with their users; they will encode ' ' to %20 for them. Firefox will display spaces, but Chrome will still displays the %20's in the address bar. However the underlying issue is that the spec isn't going away, and a %20 in the url, which is still required, is not "human friendly". People cut and paste urls outside of these browsers, and both Firefox and Chrome will still encode the urls correctly.
3

You can't change which characters the UrlEncode method uses, the use of "+" for spaces is defined in the standards for how an URL is encoded, using "-" instead would mean that the method would change the value and not just encoding it. As the "-" character is not encoded, there would be no way to decode the string back to the original value.

In your method, there is no need to check for the character before doing the replacement. If the Replace method doesn't find anything to replace, it just returns the original string reference.

public static string EncodeForSEO(this UrlHelper helper, string unencodedUrl) {
   return helper.Encode(unencodedUrl.Replace(' ', '-'));
}

9 Comments

Thanks for the code fix. It's more than just encoding, specifically it's making the url parameters more SEO friendly. On the controller side though a call to HttpUtility.Decode will decode any parts of the parameters that were encoded normally.
Hm... Why would "cast-the-musical" be more SEO friendly than "cats the musical"?
Who knows, I was told it was the google-preferred way by an SEO specialist at a talk I attended. Stack Overflow seems to do the same thing too.
The -'s are a lot nicer on the eyeballs, too.
-1 spaces are fine. modern web browsers don't even show them as anything other than spaces. @Robert Paulson is correct with UrlPathEncode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.