Forum Moderators: phranque

Message Too Old, No Replies

Rewrite ONE word, getting other variables

Writing a rewrite rule that affects only ONE word, but get more variables

         

DarkEden Genesis

2:28 am on May 2, 2012 (gmt 0)

10+ Year Member



Hello,

I'm working on a rewrite rule and I've faced a problem.

That's how my system works:
url.com/index.php?action=news&type=$type&id=$id

$id = unique id of the notice
$type = updates, news, events, etc...


  • DESIRED URL:
    url.com/$TYPE/$title-of-the-notice


    So far ACQUIRED URL:
    url.com/$TYPE/$ID/$title-of-the-notice

    $title = notice title

    Rewrite rules:
  • RewriteRule updates/([^/]+)/([^.]+) index.php?action=news&type=0&id=$1&title=$2
  • RewriteRule notices/([^.]+)/([^.]+) index.php?action=news&type=1&id=$1&title=$2
  • RewriteRule events/([^.]+)/([^.]+) index.php?action=news&type=2&id=$1&title=$2



    How can I do my desired url works like I want?
    Am I wrong or do I need to keep ID as variable in order to find it and then rewrite?
  • lucy24

    7:24 am on May 2, 2012 (gmt 0)

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



    Am I wrong or do I need to keep ID as variable in order to find it and then rewrite?

    You can't capture something that doesn't exist, if that's what you mean. All your examples involve two captures, which are turned into two items in the query string.

    There is no way to tell mod_rewrite that "updates" = 0, "notices" = 1 and so on. If there are too many to list manually, you would have to rewrite to a php script that looks them up.

    But why would you want to, when the whole thing is going straight to php anyway? The long url with the query string is invisible to the user, so just let it go through as

    RewriteRule (updates|notices|events)/([^/]+)/([^.]+) index.php?action=news&type=$1&id=$2&title=$3 [L]

    and so on, and let your php take it from there.

    g1smd

    7:26 am on May 2, 2012 (gmt 0)

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



    There's one very important thing to test.

    Request a page of your site, then change one thing in the address bar of your browser. Swap the first word to one of the other options (updates, notices, events) and check that the new URL request returns a 404 response. Failure to do this is a common source of duplicate content.

    You should include the ID number in your URL but NOT as a folder level. I would have it just before the name and separated from it with a hyphen.

    lucy24

    8:26 pm on May 2, 2012 (gmt 0)

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



    I would have it just before the name and separated from it with a hyphen.

    So user sees
    /0-blahblah/
    /1-foobar/
    /2-widget/
    etc?

    Hm. Personally I think it would look friendlier the other way around:
    /blahblah-1/
    /gizmo-2/
    etc.

    Or did you mean "the text equivalent of the ID number"? I think it depends on whether the "title" bit really does contain hyphens or if the OP just said it that way. I understand that you wouldn't want a single hyphen-delimited element stuck to the end of something containing a random number of hyphens.

    But then, I may simply be allergic to hyphens ;) They always look spammy to me. Breathless, like "look-what-a-deal-we-have-for-you".

    g1smd

    8:52 pm on May 2, 2012 (gmt 0)

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



    It might look nicer the other way round, but the RegEx pattern with ID at the front is much easier to code, and runs a heck of a lot quicker.

    ^([0-9]+)-(.*)


    ...rather than "hunt the final hyphen and capture all before it as the name and all after it as the ID".

    I usually use a single letter before the ID, p for products, r for reviews, c for categories, etc.

    DarkEden Genesis

    9:00 pm on May 2, 2012 (gmt 0)

    10+ Year Member



    There is no way to tell mod_rewrite that "updates" = 0, "notices" = 1 and so on. If there are too many to list manually, you would have to rewrite to a php script that looks them up.

    Actually there are only that three new types.
    So, I can call these types like:
    0 = updates
    1 = notices
    2 = events

    There is nor problem to write it manually since it's just three for now.(is that what you meant to say?)

    The point is that I don't want the ID of the notice.
    I only want:
    notice type and title itself

    Like this:
    url.com/updates/update-of-the-day



    Request a page of your site, then change one thing in the address bar of your browser. Swap the first word to one of the other options (updates, notices, events) and check that the new URL request returns a 404 response. Failure to do this is a common source of duplicate content.

    You should include the ID number in your URL but NOT as a folder level. I would have it just before the name and separated from it with a hyphen.


    G1smd, you are right it's getting duplicated content.
    When I change notice into update or event, it stills open.

    url.com/notices/10/notice-title
    replaced for
    url.com/UPDATES/10/notice-title
    STILL OPENS


    What must I do?


    Also you said to include the ID into the URL.
    Isn't there anyway to avoid that?
    Since I'm willing to get rid off that long query string URL and make it looks clearer, why should I keep an ID?

    g1smd

    9:39 pm on May 2, 2012 (gmt 0)

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



    To fix the "wrong type of page for this ID" error, you should first capture the page type into $1. Next, as one of the first things in your script look in your database to see what type of page this ID should be. If it is wrong, you could either return a 404 status and error message or you could redirect to the correct URL. Of course you already have a routine that returns 404 for non-valid IDs.


    You could get rid of the id, and simply use the page name as the database record key. There's many pitfalls in that method. A numeric ID will be unique per page and easy to pattern match. It's efficient and easy to code.

    I employ a system where URL looks like /p234-page-name but should someone request /p234-corrupted-name the system automatically issues a redirect to the correct URL. This also allows the page name to be altered (and hence the URL) at any time and for all old external incoming links to be redirected and for all on-page links within the site to automatically use the new URL right away.