SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ModRewrite if /search/ appears in URL

    Hi,

    I have a number of links such as /search/texas /search/colorado

    I am trying to write a modrewite tool which will test the URL and if it contains /search/ then it will redirect to /search/index.php.

    Any pointers?

    Bit stuck!

    K

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

    Have a look at the tutorial linked in my signature. It addresses problems like that ... and I'll be here to answer questions you might have.

    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 Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks David,

    I've got it to redirect the main directory but not the sub directories:

    RewriteRule ^search/(.*)$ /search.php/$1 [L]

    Any advice?

    Kyle

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

    Oh, more meat for DK's meatgrinder!

    What is that supposed to do? All I can see is that it matches search/ followed by {garbage} and assumes that search.php/ is a directory within which {garbage} can be found (and served). Okay, that's possible with Options +MultiViews (which serves search.php which must then parse the path for {garbage} and do something useful with it).

    Why not start at the beginning (again) and code "I am trying to write a modrewite tool which will test the URL and if it contains /search/{state} then it will redirect to /search/index.php." Hmmm, that means that you were close but ...
    Code:
    RewriteEngine on
    RewriteRule ^search/(.+/)$ search.php [L]
    Okay, okay, I read between the lines to dig out that you're trying to match state names and will probably use that in your search/index.php file to pull state info from a db. If you're only trying to match a couple of states, e.g., texas and colorado, then the following would be more appropriate:
    Code:
    RewriteEngine on
    RewriteRule ^search/(texas|colorado)/$ search.php [L]
    Enumerating the strings which constitute an acceptable match is the best thing that you can do (to protect your code).

    Regards,

    DK
    Last edited by dklynn; Aug 27, 2010 at 03:41. Reason: Trailing / - ARGH!
    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 Enthusiast
    Join Date
    Apr 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If the URL contains /search/ then it needs to redirect to search.php. From the search.php page the PHP code will then process the page based on the URL - the PHP code will ready the URL.

    So if url is /search/texas/ then it will go to search.php and does not need to pass any variables to it.

    If the url is /something/ then nothing needs to happen. It is only if a /search/ directory exists.

    Does that make sense

    K

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

    Well, not really. What is "something"? Can it be "search"? If not, then the "search/" in the regex will cause it NOT to match "something" and there will be no redirection.

    Worse, you've changed your redirection from search/index.php to search.php. Which is it?

    Finally, is PHP reading the Apache variable {THE_REQUEST} to get the requested URI (not the redirection)? That makes this example trivial!

    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
  •