SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Static To Dynamic URL using Modrewrite

  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Static To Dynamic URL using Modrewrite

    Hi,
    I have site running and wish to convert the url
    http://mysite.com/variable
    to
    http://mysite.com/index.php?action=variable

    This requirement is opposite to the usual method to convert dynamic url to static url

    How can this be achieved with mod rewrite rules
    http://kkonline.org - Inspiring Life...

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

    I'm here to help members learn to create good mod_rewrite code (rather than offer free coding) so I'll ask you first to read the tutorial linked in my signature then make a valid attempt. Post your code and I'll be happy to critique it and offer additional pointers to get you where you want to go.

    BTW, that's basically what WordPress's mod_rewrite does and they do a good job despite a few missteps that noobies might not be aware of. Look through this board for examples (and my critique).

    Regards,

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

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Below is what I have written

    RewriteEngine On
    RewriteRule ^index\.php\?data\=variable$ /variable [L]

    And I wish to request the following from browser:
    http://example.com/variable/

    to get redirected to
    http://example.com/index.php?data=variable

    However this doesn't redirect
    http://kkonline.org - Inspiring Life...

  4. #4
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,795
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by krishnakhanna View Post
    Hi,
    Below is what I have written

    RewriteEngine On
    RewriteRule ^index\.php\?data\=variable$ /variable [L]

    And I wish to request the following from browser:
    http://example.com/variable/

    to get redirected to
    http://example.com/index.php?data=variable

    However this doesn't redirect
    Your rewrite rule is backwards (or so it seems)
    Since you want to capture /variable/ and redirect it to index.php?data=variable, you want to do the following
    Code:
    RewriteEngine On
    RewriteRule ^variable/? index.php?data=variable [L]
    Then when you want to make it capture /*/ and redirect to index.php?data=*, you can do the following
    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)/? index.php?data=$1 [L]

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

    Good reply except that I'd prefer to check that the request is not for a directory (or extensionless file) before your RewriteRule. That's important as not to do so could disable important functionality.

    krishna,

    If you read the tutorial, you'd have known that your attempt was "backward" as cp noted. It's worthwhile to spend the time to read and understand the tutorial and look through the sample code (and explanations) at the end.

    Regards,

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

  6. #6
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,795
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    cp,

    Good reply except that I'd prefer to check that the request is not for a directory (or extensionless file) before your RewriteRule. That's important as not to do so could disable important functionality.
    Bah! I knew that! I can't believe I forgot it. Next time I'll nail it!

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

    Not bad anyway!

    Regards,

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

Tags for this Thread

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
  •