Forum Moderators: phranque

Message Too Old, No Replies

Unique mod rewrite rule needed

mod_rewrite search replace

         

marke

2:29 am on Oct 20, 2007 (gmt 0)

10+ Year Member


I've got a need for a unique mod_rewrite rule that I can't seem to figure out how to create. Basically I need to take whatever URL is received by the server and replace all spaces, plus symbols, and periods with a dash. I also need to strip out all non-alphanumeric chars (e.g. remove special chars and other punctuation).

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

marke

3:00 am on Oct 20, 2007 (gmt 0)

10+ Year Member



Alternatively I could rewrite the URL from this:

[host...]

To this:

[host...]

Any idea how to make a rule to do that? I tried this and it doesn't work:

RewriteRule /articles/show/([0-9])$ /articles/show/$1

Any ideas where I'm writing the rule wrong?

civgroup

5:20 am on Oct 20, 2007 (gmt 0)

10+ Year Member



Thanks for the help over there. Maybe I can return the favor:

RewriteRule ^articles/show/([0-9]*) http://www.example.com/articles/show/$1 [R=301]

marke

5:43 am on Oct 20, 2007 (gmt 0)

10+ Year Member



I tried your rule and these:

RewriteRule ^/articles/show/([0-9\-]*)$ /articles/show/$1
RewriteRule ^/articles/show/([0-9]*)$ /articles/show/$1
RewriteRule ^/articles/show/([0-9]+)$ /articles/show/$1

And none work. I'm not using a RewriteCond statement if that makes any difference.

civgroup

5:50 am on Oct 20, 2007 (gmt 0)

10+ Year Member



That's odd - I tested that rule I gave and it worked on my server. :-/

Sure you copied it exactly?

marke

5:54 am on Oct 20, 2007 (gmt 0)

10+ Year Member



Here's what I got to finally work. I'd still rather replace chars as I wrote in the initial post. If anyone knows how to do that with mod_rewrite please post a suggestion.

RewriteRule /articles/show/([0-9]*)-(.*)$ /articles/show/$1 [R=302,L]

akameng

10:21 am on Oct 20, 2007 (gmt 0)

10+ Year Member



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

ashishp

6:17 pm on Oct 20, 2007 (gmt 0)

10+ Year Member



To make it more safe, just rewrite it to a script like articleshow.php

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.