Forum Moderators: phranque

Message Too Old, No Replies

Simple Rewrite Question

         

chasehx

7:52 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



I have a rewrite rule in my .htaccess file that reads like this:

RewriteRule ^article/[a-z]+/([0-9]+)$ news.php?id=$1

the point, is for it to rewrite like this /article/snipped-article-title/23

the 23 being of course the articles ID.

I am scratching my head though because the rewrite rule will work for

/article/snippedarticletitle/23

As soon as I add a hyphen to the URL it breaks the rewrite. The article title is not used for a GET variable or anything, it is just junk for Google. I looked but I can't figure out how to remedy the hyphen problem. I am sure it is something pretty simple. Someone want to point me in the right direction?

g1smd

10:43 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Firstly, you need [L] on the end of the rule.

Secondly, the 'allowed character list' (i.e. the list in the

[...]
) does not include a hyphen.

Be aware that because you do not check the value of $2 anywhere, that your site can be fake-URL-hijacked.

You link to

example.com/article/34567/this-great-product
but someone else links to
example.com/article/34567/overpriced-unsafe-junk
and your site will serve the page as Duplicate Content with a "200 OK" status code.

jdMorgan

11:01 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So in a nutshell, you need something like:

RewriteRule ^article/([^/]+)/([0-9]+)$ news.php?id=$2&title-to-be-validated-against-database=$1 [L]

so that the title can now contain any character except for a "/", and the title is passed to your script to be checked against the correct title in your database for that article id number.

If the title in the requested URL does not agree with the one stored for that article id number, then instead of serving the page for that article id, your script should generate a 301-Moved Permanently redirect to the /article URL with the (correct) title that corresponds to that article id number.

Jim

chasehx

12:41 am on Dec 10, 2009 (gmt 0)

10+ Year Member



Thank you so much for your help guys. I will make the changes to my .htaccess file when I get to work tomorrow. Thanks for the tip about the URL checking, now that I think of it that was a pretty stupid oversight..

jdMorgan

1:04 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, that's an extremely-common oversight. :)

And some very-expensive and/or very popular script packages contain plenty of them...

Jim

g1smd

8:45 pm on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... and were told about it a decade or more ago and still choose to inflict the problems on to their users to the present day.