SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod rewrite question

    Hi

    I've been looking around on the internet and playing around a bit and managed to get this working perfectly:

    Code:
    RewriteRule /sites/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+) /sites/index.php?sid=$4
    This takes a url of the form sites/Some-Country/Some-City/Some-Site/siteid and re writes to the dynamic page sites/?sid=siteid

    But the next part I'm having some trouble with: I need a url of the form
    Code:
    sites/Some-Country/Some-City/Some-Site/siteid?parameter1=somevalue&parameter2=somevalue
    to be re written to the same as earlier but with those extra url parameters. I've had no success yet and any help would be appreciated!

    Also, while I'm about it, I need what I've currently got at /([A-Za-z0-9-]+) to also match foreign characters (such as ) - how do I do this?

    Thanks
    -------------------------------
    http://www.divinglogs.net
    The free online diving community

  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)
    ben,

    For your first question, the QSA flag is what you're looking for, i.e.,
    Code:
    RewriteRule /sites/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)$ /sites/index.php?sid=$4 [QSA,L]
    I've added the end anchor to fetch the digits from the fourth atom but require them to be the last thing in the URI, the Last flag because it's important (to mod_rewrite) and the QSA flag to retain any pre-existing query string.

    As for your second question (foreigh characters), you're not needing the $1 - $3 atoms so merely use ([^/]+) for each one. Otherwise, you've got to add the accented characters to a character range definition (e.g., [a-zA-Z0-9]+ to add your "" character). Obviously, more than one character makes this a ridiculous solution to the "NOT /" would be the preferred solution.

    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
    Join Date
    Oct 2009
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much, works beautifully! Really appreciate the help.
    -------------------------------
    http://www.divinglogs.net
    The free online diving community

  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)
    ben,

    Which suggestion worked for you?

    Okay, neither should work if you're using Apache 2.x as the leading / in the RewriteRule will be rejected by mod_rewrite. The leading / in the redirection could also be redirected to the sites directory of the server's root. In other words, if you're using Apache 2, delete the leading / in the regex; if you're using either (and the .htaccess is in the DocumentRoot of the domain), delete the leading in the redirection. Sorry I didn't catch that earlier.

    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

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used your first suggestion but I also changed the bits in between the /'s to [^/]+ as, like you said, I don't need those atoms so this caters for all characters.

    I am running Apache 2, and it seemed to work fine! This is on my localhost though where I've been testing it and I've put it in the httpd.config file. However, I've just found its not working on my live site (also running Apache 2), where I put it in the .htaccess file (in the public_html folder). I think its a problem with loading modules though, as when I put
    Code:
    LoadModule rewrite_module modules/mod_rewrite.so
    at the top of the file, I get a server error. I'll ask my host about that though.
    -------------------------------
    http://www.divinglogs.net
    The free online diving community

  6. #6
    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)
    ben,

    The LoadModule directives are reserved for the httpd.conf (or equivalent). Use the test in my signature's tutorial article to confirm that mod_rewrite is enabled and properly functioning.

    The comment about the leading / in the regex was specifically aimed at Apache 2 installations:

    Apache 1.x requires the leading / - ^/
    Apache 2.x rejects the leading / - ^
    Apache 1.x and 2.x accept - ^/?

    The leading / in the redirection is interpreted FIRST as asking for a file relative to the server's root directory THEN it will look to the DocumentRoot of the domain. Omitting the leading / in the redirection - especially when in the .htaccess in the DocumentRoot - is highly recommended.

    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
  •