Forum Moderators: phranque

Message Too Old, No Replies

SE Friendly URLs - Changing plus sign to underscore

         

energylevel

6:24 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



RewriteRule (.*)-(.*)\.html$ search_results.php?make=$1&model=$2

some of the make model values can be double barreled names like

search_results.php?make=make+name&model=model+name so therefore required static looking url

make+name-model+name.htmlQuestion is how to I replace the + sign with an inderscore?

jdMorgan

9:20 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Short answer: Change all the links on your pages (that is the only way to change the URL seen by search engines), then modify your script to accept those new URLs. Then add code (either in .htaccess, the server config files, or in your script) to redirect the old URLs to the new ones.

The RewriteRule you posted is not involved does not need to be changed.

I am aware that due to very sloppy use of language on the Web, the phrase "use mod_rewrite to change your URL" is common. But in fact, mod_rewrite cannot change a URL, because the URL is defined by a link on a page.

Mod_rewrite *can* be used to externally redirect a URL, but that does not change the original URL.

Mod_rewrite can also be used to associate any file on your server with any URL requested from your server -- That is to say, "If this URL is requested, go get (or generate) the content from that file." This is termed "an internal rewrite."

So, two important points are that a URL is not a file and a file is not a URL -- They are separate and different things, and an external redirect and an internal rewrite are also two different things.

Jim

energylevel

10:26 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



Thanks links are already on the page, I now what mod_rewrite does, I'm looking at the best solutions to handle the + sign ....

jdMorgan

12:17 am on Jul 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The best solution is to handle that in your script. mod_rewrite is very poor at replacing characters, because it has to do them one at a time or in batches. If you cannot modify your script and must use the mod_rewrite option, we've had many threads on the subject, so try searching WebmasterWorld for "replace underscore hyphen rewriterule [google.com]" and similar. The method is the same, only the characters change.

You will have to escape the "+" characters in your patterns by typing "\+", though. Otherwise, the "+" characters in your patterns will be treated as regular-expressions "one or more of the preceding" quantifiers.

Jim

[edited by: jdMorgan at 1:20 pm (utc) on July 26, 2008]