Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Help re-writing urls with query strings

    I've been reading some mod_rewrite tutorials but am still having some issues with some odd URL's that I can't seem to find similar examples of.

    Here is an example of a URL, (secondary parameter is the category)

    http://www.mysites.com/results.php?&secondary=2

    I would like to change all of these to something like:
    http://www.mysite.com/cars/ford (secondary=2)
    http://www.mysite.com/cars/chevy (secondary=3)

    etc...

    I found a tutorial showing an example like this, but it's putting odd characters in the URL and the page doesn't display
    RewriteCond %{QUERY_STRING} ^&secondary=2$
    RewriteRule ^results.php?$ ^cars/ford$ [L,R=301]


    Can anyone help me out on this?

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by a4udi View Post
    Can anyone help me out on this?
    I tested this on my server and it works:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/results\.php$
    RewriteCond %{QUERY_STRING} ^&secondary=2$
    RewriteRule ^.+$ /cars/ford? [redirect=301,last]
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arbitrator View Post
    I tested this on my server and it works:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/results\.php$
    RewriteCond %{QUERY_STRING} ^&secondary=2$
    RewriteRule ^.+$ /cars/ford? [redirect=301,last]
    Thanks for having a look. This is similar to something I had working before, it is doing the rewrite properly in the browser address bar, but it goes to a blank page. So I'm not sure if it's not pointing to the write page or what.

    I think my problem was the fact it has the "?" and "&" in the url string... not sure if the code above accounts for that?

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by a4udi View Post
    Thanks for having a look. This is similar to something I had working before, it is doing the rewrite properly in the browser address bar, but it goes to a blank page. So I'm not sure if it's not pointing to the write page or what.

    I think my problem was the fact it has the "?" and "&" in the url string... not sure if the code above accounts for that?
    The code assumes an origin URL http://example.com/results.php?&secondary=2 with the part after "http://example.com/" requiring an exact match (including the superfluous ampersand).

    It redirects to http://example.com/cars/ford. Of course, for this to work, there has to actually be something at that URL; this isn't simply a URL transform. You'll need a file or folder named "ford" in a "cars" directory. In the case of a folder, a default file will need to be present, and you'll need another RewriteRule to ensure that index.html, default.html, etc. can't appear in the URL.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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