SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot s21825's Avatar
    Join Date
    Oct 2003
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite issue with query sting

    Hi All,

    I'm trying to change a URL for a series of pages from a query string based URL to a "pretty" URL. The current URLs look like this:

    http://www.example.com/catalog/index...cturers_id=190

    And I want to make them like this:

    www.example.com/manufacturer

    I have a rule that makes the new URLs active:

    RewriteRule ^mfrone$ /catalog/index.php?manufacturers_id=190 [QSA,L]
    RewriteRule ^mfrtwo$ /catalog/index.php?manufacturers_id=198 [QSA,L]
    ... etc ...

    Now I want all the existing links on the site and incoming links in the old format to redirect to the new format (without an infinite loop).

    I'm trying this but it does not seem to have any effect at all:

    RewriteCond %{REQUEST_URI} ^/catalog/index\.php$
    RewriteCond %{QUERY_STRING} manufacturers_id=190
    RewriteRule (.*) http://www.example.com/mfrone [R=301,L]

    I'm hoping a fresh set of eyes (and brains) will make me see my folly!

    Thanks!
    s21825

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    s,

    Been there, done that, answered that and included that in my signature's tutorial. Please have a read THEN ask questions on implementation.

    Suggestion: Use unique manufacturer names rather than the digits as you MUST use one or the other in the regex to make any sense out of redirections.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Zealot s21825's Avatar
    Join Date
    Oct 2003
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the lead DK.

    I've been reading your tutorial while I click refresh to see if someone responded to the thread

    So I've gleaned the following tidbits:

    RewriteCond %{IS_SUBREQ} false
    RewriteCond %{REQUEST_URI} ^/catalog/index\.php$
    RewriteCond %{QUERY_STRING} manufacturers_id=190
    RewriteRule .? http://www.example.com/mfrone [R=301,L]

    The "not capturing what you don't need" tip is useful as is the IS_SUBREQ test string to prevent loops.

    However, I'm still not getting any effect from these rules. It's like they are not even there. I wasn't even to the point of getting an infinite loop because the rewrite wasn't happening (for some unknown reason). I think it should work, but it doesn't and I don't know why.
    s21825

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    s,

    You missed the one critical point at the beginning: YOU create the new format for the link and let mod_rewrite redirect to the servable URI.

    What does mfone serve? My guess is NOTHING!

    Drop back 10 but don't punt - PLAN! If you want cat/IBM as the URI (cat would be necessary only if you want to have other links to redirect), it's easy to create regex to catalog/index.php?manufacturers_name=IBM. If you prefer to use the IDs, then CREATE YOUR LINKS LIKE cat/90 but that's hardly an improvement, is it? One point here is that the NEW URI needs to contain the information to use to convert it to the REAL URI.

    Code:
    RewriteEngine on
    RewriteRule ^cat/([a-zA-Z_]+)$ catalog/index.php?manufacturer_name=$1 [L]
    Notice the direction of the redirection?

    I seem to remember (Or am I losing it? Oh, better not answer that question!) that you also want to redirect TO the new format. That would add (before or after) the above RewriteRule:

    Code:
    RewriteCond %{IS_SUBREQ} ^false$
    RewriteCond %{QUERY_STRING} manufacturers_name=([a-zA-Z_]+)
    RewriteRule ^catalog/index\.php$ cat/%1 [R=301,L]
    Yes, I do have a hang-up about using names or titles or ... rather than digits to use to query the database. Just make sure that whatever you use is UNIQUE (which can be enforced by MySQL) in the database.

    Another thing you (might have) missed is that Apache 1.x requires ^/ but Apache 2.x requires only ^. To allow either to match, you need to use ^/?. Because there are so few Apache 1's left in the wild, I assumed Apache 2 in my code.

    Please REread the tutorial as that should make the above code obvious.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •