1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

.htacess RewriteRule to include "&" char into variables.

Discussion in 'Apache' started by Davidf25sc, Oct 3, 2008.

  1. #1
    Hello,

    What im trying to do is to use RewriteRule to have custom urls, what i want is having something like http://www.domain.com/the_name and using htacess make it go to show.php where "the_name" will be a variable for show.php, what i have works well except when the url contains the "&" char, this causes show.php to get the variable only until the part where the "&" char comes, i.e. for "/the_&_name", will only get "the_"

    This is what i have :
    Link : http://www.domain.com/parse_&_this

    .htaccess :
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)$ show.php?name=$1 [L]
    and also tried with :
    RewriteRule ^([a-z,_,&]*)$ show.php?name=$1 [L]

    show.php :
    <?php
    echo $_GET['name'];
    ?>

    ---------
    This works fine for all names except the ones with the "&", does any body knows how this is done?

    Example :
    http://en.wikipedia.org/wiki/Command_&_Conquer

    Any help is appretiated.
    Regards,
    David.
     
    Davidf25sc, Oct 3, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, GET variables are separated by '&' characters.
    So, to get the "variable" you need, you cannot use the $_GET/$_REQUEST array.

    Try this:

    show.php:
    <?php 
    echo $_SERVER['QUERY_STRING'];
    
    PHP:
    .htaccess:
    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)$ show.php?$1 [L]
    
    Code (markup):
     
    keyaa, Oct 3, 2008 IP
  3. Davidf25sc

    Davidf25sc Active Member

    Messages:
    165
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thank you very much for the reply keyaa, it looks like a good way to do it.
    I've also found a way to do it playing a bit with .htacess :
    ------------------------
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^([^&]+?)\&([^&]+?)$ $1\%26$2
    RewriteRule ^([^&]+?)\&([^&]+?)\&([^&]+?)$ $1\%26$2\%26$3
    RewriteRule ^([^&]+?)\&([^&]+?)\&([^&]+?)\&([^&]+?)$ $1\%26$2\%26$3\%26$4
    RewriteRule ^show-(.*)/$ show.php?name=$1 [L,QSA]
    ------------------------
    The 3rd line will check the url to see if there is one ampersand character, the next line will catch the ones with 2 ampersands and so on, finally the last line will give show.php the correct get variables.

    This will work with most uncomon chars in the url, not all, i had a name with "+" character that didn't worked, this is because "+" is interpreted as a space.

    Regards,
    David.
     
    Davidf25sc, Oct 7, 2008 IP