Forum Moderators: phranque
So for example, a URL might look like this:
http://host/articles/show/123456-this+is+the+title:+and+this+is+part+too
or this:
http://host/articles/show/789123-this+is+a+title+of+an+article
And I need it to be rewritten to this:
http://host/articles/show/123456-this-is-the-title-and-this-is-part-too
I can't figure it out. Anybody know how to do this while keeping in mind that the URL length could vary but the basic format remains the same, thus:
http://host/articles/show/[articleID]-[article title string]
Any help would be greatly appreciated
RewriteRule /articles/show/([0-9]*)-(.*)$ /articles/show/$1 [R=302,L]
I suggest this:
RewriteCond %{REQUEST_URI} /articles/show/([0-9]+)-(.+)$
RewriteRule /articles/show/([0-9]+)-(.+)$ /articles/show/$1
May be the ([0-9]*)-(.*) not good, because it's look for
([0-9]*)=[0-9]{0,} --> existing of any number zero or more times, but you want to match a number not zero match! ( empty )
and then - match exactly -
and .* =.{0,} match any character repeated zero or more times,
but + match one or more times,
(in this case /articls/show/- matched due to your syntax ) and it's wrong.
I am sorry for any error, Just I wanna to help
The assuming 123456 is the id it becomes:
RewriteRule /articles/show/([0-9]+)-(.+)$ /articleshow.php?id=$1 [L]
Meaning: if the url starts with /articles/show and has one or more digits followed by a hyphen and then one or more characters or digits or anything, then pick up the digits after /show/ and till before the hyphen (the brackets) and pass it to the script articleshow.php
The browser will still show:
[host...]
or
[host...]
but execute
http://host/articleshow.php?id=123456
the url and the actual file have no relation to each other, they can be totally different.
You do not want a redirect (R=302) because it wil change the url that is shown in the browser the above regexp will do the translation transparently (invisible to the user).
If you want the hyphens to show in the SERPs then simply use hyphens to link to the article.
For linking you should use [host...]
HTH.