mod_rewrite and query string

Posted on 2006-03-29
Last Modified: 2010-03-04
Question by:us111
    LVL 27

    Assisted Solution

    So you're planning to use the RewriteRules in a .htacccess file? Or in httpd.conf?

    That would be for a .htaccess file:

    RewriteEngine on
    RewriteRule ^gallery/([^/]+)/index\.html$ /index.php?gallery=$1 [L]
    RewriteRule ^gallery/([^/]+)/([0-9]+)/index\.html$ /index.php?gallery=$1&img=$2 [L]

    for httpd.conf, just add a leading slash in the pattern:

    RewriteEngine on
    RewriteRule ^/gallery/([^/]+)/index\.html$ /index.php?gallery=$1 [L]
    RewriteRule ^/gallery/([^/]+)/([0-9]+)/index\.html$ /index.php?gallery=$1&img=$2 [L]
    LVL 23

    Accepted Solution


    More Advanced - the Query String:

    The query string is passed in separately from the URL. This means that a simple regex doesn't necessarily do the trick, but a compound statement using RewriteCond (condition) is required.

    RewriteCond %{QUERY_STRING} id=([^&;]*)
    RewriteRule ^/$ http://%{SERVER_NAME}/%1/? [R]
    RewriteRule ^/([^/]*)/?$ /index.php?id=$1 [L]

    The RewriteCondition matches only when the following condition is true, and continues until a "last" [L] is stated. The Condition's backreferences are different, using the % prefix, and their scope lasts beyond the Condition line.

    This above example would translate "/?id=home" into "/home/", and then re-assign the value of "home" to the id HTTP_GET_VAR. One more thing to notice here is that the the second line has a trailing ? - this is used to negate copying of the query string into the new, re-directed URI.

    More Reference Links:

    LVL 8

    Author Comment

    >>>it is now classified as abandoned
    No, sorry but leave this question opened for the moment

    LVL 15

    Expert Comment

    Is there still a problem that we can assist you with?  What happened when you followed the advice given by the experts above?
    LVL 8

    Author Comment

    No no I think it's ok. But I need to test...
    LVL 8

    Author Comment

    Please leave it opened.

    I know you have to clean but this question has only 3 month. I think it's better to clean questions from 2005!

    I'm gonna do my best to close it.  btw i will increase the points
    LVL 8

    Author Comment

    I'm agree with your arguments

    Anyway, as I have no time to test I suppose it works......

    Points splited

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    nginx reverse proxy 6 71
    Stop people from trying to get into my server 8 96
    How to analyze web traffic logs 10 77
    PHP Error Settings 12 67
    It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
    In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now