Forum Moderators: phranque

Message Too Old, No Replies

how to use hyphen in URL

how to use hyphen in URL

         

Anne3G

7:02 am on Oct 20, 2010 (gmt 0)

10+ Year Member



Hi,

i have these kind of urls....

site.com/categoryName-brandName-modelNumber.html

URL should be like this:
site.com/mobile-phones-sony-ericson-phones-es-995.html

Here mobile-phones is categoryName, sony-ericson-phones is brandName & es-995 is modelNumber.

I set this rule in htaccess file
RewriteRule ^([a-zA-Z0-9-]+)-([a-zA-Z0-9-]+)-([a-zA-Z0-9-]+)\.html$ index.php?catg=$1&brand=$2&model=$3 [NC,L]

Its not working properly & I got below output at the time of reading parameters.

Array
(
[catg] => mobile-phones-sony-ericson-phones
[brand] => es
[model] => 995
)

Please do the needful in this regards.

jdMorgan

1:13 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot "divide up" a requested URL-paths into substrings using a "separator" character that can also appear in any of the substrings. Apache has no idea whether the category, brand name, or model number might contain a hyphen, so you cannot use a hyphen as the separator to parse out the variables in a deterministic manner.

A better solution would be to use slashes to separate your "fields" and then you can use hyphens within those fields:
Example URL:
 site.com/mobile-phones/sony-ericson-phones/es-995.html 


Example Rule:
 RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)\.html$ [NC,L] 


Note the escaping on reserved characters, and that the use of [NC] allows [a-zA-Z] to be shortened to [a-z].

There is one downside to using slashes: The links to other pages and to included objects on each page must now be server-relative or absolute. In other words, you must use <img src="/images/logo/my-logo.gif"> or <img src="http://site.com/images/logo/my-logo.gif"> instead of <img src="images/logo/my-logo.gif"> on your pages.

An alternative is to use some other character allowed by the HTTP protocol as a separator, such as "." or "," or "~", but the slash looks most "natural" and will allow for keyword-in-URL indexing and highlighting in the search results. Be very sure that you understand these effects on search engine listing and click-through rates before using a non-slash character...

Jim

Anne3G

1:44 pm on Oct 20, 2010 (gmt 0)

10+ Year Member



Thanks Jim.