SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with .htaccess with query strings

    Hello,
    could somebody please help me as to how I can REDIRECT from a page with query strings to a page with other query strings?
    Apparently, the following doesn't work:

    Redirect /folder/file.php?page=contact http://mysite.com/somethingelse/othe...var=otherquery

    Thanks for your suggestions.

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

    [QUOTE=marcnyc]
    could somebody please help me as to how I can REDIRECT from ...

    Redirect /folder/file.php?page=contact http://mysite.com/somethingelse/otherfile.php?var=otherquery

    That's a pretty easy one (if you're using the correct syntax):

    Code:
    RewriteEngine On
    RewriteRule ^folder/file\.php$ http://mysite.com/somethingelse/otherfile.php?var=otherquery [L]
    First, it's a RewriteRule.
    Second, you don't need (and probably shouldn't have) the leading slash in the {REQUEST_URI} and SHOULD escape the dot in the file name.
    Third, the query string is not treated as a part of the {REQUEST_URI}.
    Fourth, if you're in your own site, you don't need the http://mysite.com/.

    I hope this helps.

    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
    Dec 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David, thanks for taking the time and sorry for getting back to this so late (been busy with work)...
    So, my problem is this:
    I need to redirect all traffic to a page with a certain query to a different page with a different query. Hence, if I understood correctly, your suggestion won't work/apply in my case because it redirects all traffic to folder/file.php, regardless of its query string... My so-called file.php will display different pages according to the query it has, so I need to re-direct only if a certain query is present... in other words, if folder/file.php?query=1 I want to redirect to otherfolder/differentfile.php?differentquery=something else, but if the user goes to folder/fle.php?query=2 then I don't want anything to happen at all, do you know what I mean?
    Thanks for your time. Hopefully you can still help.

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

    I thought your "problem" was too simple to be real!

    Oh, well, you can use RewriteCond(itions) to examine the {QUERY_STRING} and establish your RewriteRule(s) accordingly, e.g. (UNTESTED):

    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^query=1$
    RewriteRule ^(.*) otherfolder/differentfile.php?differentquery=something [L]
    RewriteCond %{QUERY_STRING} ^query=7$
    RewriteRule ^(.*) othersub/different.php?anotherquery=whatelse [L]
    # nothing required if nothing is to happen, i.e., query=2
    Okay, now that you stated that the contents of the query string is what you're using to determine the redirection, it makes more sense. I trust that you can make this work with these examples to work from.

    Quote Originally Posted by Apache mod_rewrite Page
    Query String

    The Pattern will not match against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just the question mark. To combine a new query string with an old one, use the [QSA] flag.
    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
    Dec 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David, it may not be rocket science but I can't say I quite get it...
    Based on my limited knowledge of these things it seems to me that you are suggesting to use 1 as a backreference for the query string and that I'd have to replace the capital words QUERY_STRING with with my query string I want to match. Did I interpret that correctly? I tried but it doesn't work... besides I don't want/need to re-use the matched query string...
    Here is the real scenario I am dealing with.
    I need all traffic from:
    /content/?option=com_smf_registration&task=register
    to be re-directed to:
    /?page=registration
    Would you be kind enough to mold your examples to apply the to my case? That'd be great... Thanks.

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

    Naw, NOT rocket science and hardly even computer science.

    The reference to the Apache mod_rewrite Page was to entice you to go read 'cause it's got a lot of useful information. Specifically, you can address a server variable known as QUERY_STRING to read the value it contains.
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^query=1$
    RewriteRule ^(.*) otherfolder/differentfile.php?differentquery=something [L]
    RewriteCond %{QUERY_STRING} ^query=7$
    RewriteRule ^(.*) othersub/different.php?anotherquery=whatelse [L]
    # nothing required if nothing is to happen, i.e., query=2
    WILL work for you if placed in the root .htaccess file as suggested above (provided you have mod_rewrite enabled as discussed in the SEO article I also made reference to).

    Above, RewriteEngine On may/may not be needed but is good coding practice 'cause it let's Apache know what's coming next.

    RewriteCond %{QUERY_STRING} ^query=1$ is a conditional (for the following RewriteRule) which examines the QUERY_STRING variable to see if it begins (^) and ends ($) with query=1. Here, query is the name of your $_GET variable and 1 is the value of that variable. IF this is true, then the next line is examined.

    RewriteRule ^(.*)$ otherfolder/differentfile.php?differentquery=something [L] takes EVERYTHING (directory and filename -- yes, I added the $ to show that's the end of the {REQUEST_URI} string) and sends it to where you redirect, i.e., otherfolder/differentfile.php?differentquery=something. The [L] flag merely tells Apache that you're done with that conditional and rule. I lacked originality in naming the otherfolder, differentfile.php filename and differentquey key and value but those were not defined in your questions.

    The remainder of the code is the same (for a different {QUERY_STRING} and redirection but you get to send it where you need it to go, too).

    Finally, the comment was that you DON'T need anything in the .htaccess to leave a {REQUEST_URI} alone.

    For your request to redirect content/?option=com_smf_registration&task=register to /?page=registration
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^option=com_smf_registration&task=register$
    RewriteRule ^content/$ /?page=registration [L]
    should do the trick (the {REQUEST_URI} is content/ and the redirection is to the root's default (index.php?) with the query string of page=registration).

    It takes a little while to get used to all this but it's well worth the time.

    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

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANKS A LOT for the explanation and the time you took.
    That makes a little more sense to me now, but, like you say yourself, it takes a little bit to get used, and when it's not one's primary job it's hard to learn and keep up with all these things...
    Your example works. I was on the wrong track, thinking I had to replace the content of your {}...
    so out of curiosity, for good practice, do I need a RewriteEngine On before every batch of Rewrite conditions and rules, or just one at the beginning of the .htaccess file?
    Anyway, thanks again very much.
    The site I was doing this for is: www.chaindlk.com - as you can see it's a non-profit hobby, I am not into IT

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

    Glad that the explanation helped -- perhaps I should have done that before.

    Aw, like anything else, it take a hands-on approach to learning AND keeping up with it. If you walk away from it for a couple of months, you'll be back to square one (but with better understanding how to "refresh").

    As for RewriteEngine On, **IF** it is required, once is sufficient.

    Glad to have been of help!

    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
  •