How can useless a link(url)?

i creation a link with .htaccess:
example: www.sakja.com/news/7/how-are-you
with:


RewriteRule ^news/([0-9]*)/(.*)$ news?id=$1 [NC,L]

now i want useless the url : www.sakja.com/news?id=anything
any idea?

Something like this?

RewriteCond %{QUERY_STRING} id=([0-9]*)
RewriteRule ^news$ http://www.example.com/news/$1 [R=301,L]

i.e. if someone tries to use the old URL style, redirect them to the new one with a 301 header.

bin,

From your “specification,” it appears that you’re wanting to change the news/7/something to news?id=7. No file extension for news? Horrors - that will cause problems (unless redirected further by a succeeding RewriteRule).

More horror, though, is the use of (.) as that is the root of all evil (in the mod_rewrite world - at least for newbies who don’t know how to tame it’s power to match everything or nothing). DO NOT EVER … NEVER … use (.) unless you know what you’re doing! Since it’s clear that you do not, DON’T - until you understand (.*) and that you can start to get a handle on in my signature’s mod_rewrite tutorial Article (the basis for this forum’s TOP sticky thread).

Dan,

301 header redirection isn’t required - unless bin needs to update the old style link. I hope that bin associates that with the R=301 flag in the RewriteRule.

Regards,

DK

That’s what I understood him to ask.

He already wrote the rule he posted to rewrite the ?id=# links to /news/#.

Now he wants to make the old links (?id=#) stop working (“useless them”).

I suggested 301’ing them to the new format.

Thank you Dan and dklynn.
Dan right said, my intention is the same. But the code does not work to post #1.
not useless the link, it still worked:
www.sakja.com/news?id=anything

bin,

With the advantage of knowing that CMS is looking for a number when the id variable is used, Dan and I both know that his ([0-9]+) will capture the digits and NOTHING more. If you want digits AND letters (and spaces), then ([a-zA-Z0-9\ ]+) will work. Note that the "\ " within a range definition will capture spaces (which are represented by %20 in URIs).

However, it is NOT a good idea to change the CMS’s code to work in this manner.

Regards,

DK