Results 1 to 22 of 22
  1. #1
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    How The Heck? ReWritten Pages and $_GET Variables
    Can you combine a rewriterule and a $_GET variable on the same page? I can't get it to work atleast.

    Here is a sample of the current ReWriteRule I am using....
    Code:
    RewriteRule   ^crap/([A-Za-z0-9-]+)/?$ crap.php?c=$1 [L]
    For example, when the user goes to....
    www . mysite . com/crap/LARGE/ the page is actually generated from crap .php?c=100

    Now, it is possible to then pass and read a $_GET variable from the rewritten page?

    Something like....
    www . mysite . com/crap/LARGE/?x=10

    Seems like a big mess.

  2. #2
    Member
    Join Date
    December 6th, 2006
    Location
    na
    Posts
    173
    Well the get variable gets posted before the page is rewritten, right? If that's the case you need to add to the rewrite to allow for that variable.. or am I missing something?

  3. #3
    Lite On The Do, Heavy On The Nuts Donuts's Avatar
    Join Date
    January 18th, 2005
    Location
    Winter Park, FL
    Posts
    6,930
    You're close to figuring it all out! Yes, but your rewrite rules aren't really on the "page", but are in your htaccess file in Unix / Apache servers (asapi in Windows machines). So when the request is made, the htaccess rewrites the url AND the vars are passed to the target page.

    At the very top of that page, program in some GETs to grab the vars you're passing.

    So your code example uses "c="...

    Add a...
    $class=$_GET['c'];

    (where "class" is the name you choose to assign to that var just as you want it to be input / received on that page. You might choose many other things besides "class", I just randomly made up a page var name).

  4. #4
    ABW Ambassador MoneyBusiness's Avatar
    Join Date
    March 14th, 2006
    Posts
    2,051
    Here's how I write my rewrite rules to pass in GET variables:

    Code:
    RewriteRule ^gifts/popular-([^/]+)-([^/]+).html$ /popular-categories.html?val=$1&pagejump=$2 [L]
    In the page called "popular-categories.html" (a php file) simply uses a call such as:

    Code:
    $val = $_GET['val'];
    $page = $_GET['pagejump'];
    Does that help?


    edit: D's too fast..

  5. #5
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    Ok, I got it. Thanks to all for the assistance.

  6. #6
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    Listen to Donuts - he has god mad crazy PHP skillz !
    ---
    This response was masterly crafted via the fingers of Patrick Allmond who believe you should StopDoingNothing starting today.
    ---
    Focus Consulting is where I roll | Follow @patrickallmond on Twitter
    Search Engine Marketing | Search Engine Optimization | Social Media | Online Video

  7. #7
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    Hi, guys.


    I got a bit different question but still related to rewriting and $_GET vars.

    I i'm trying to rewrite

    /test/reviews.php into /reviews.php?var=test

    I'm doing it with this code:

    Code:
    RewriteRule ^(test)/([^\?]+)$ /$2?var=$1
    Everything goes good until i pass something like /test/reviews.php?id=1. I want id to be passed to result page but somehow rewrite engine is not seeing further .php

  8. #8
    ABW Ambassador MoneyBusiness's Avatar
    Join Date
    March 14th, 2006
    Posts
    2,051
    Is there always a preceeding forward slash prior to the value "test" that you're trying to receive? If so, try it like:
    Code:
     ^/([^\?])/reviews.php$ /reviews.php?var=$1

  9. #9
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    nope my idea is to pass ?id=1 (e.g. everything after ?) to revrite page

  10. #10
    ABW Ambassador MoneyBusiness's Avatar
    Join Date
    March 14th, 2006
    Posts
    2,051
    Sorry about that, I read it wrong earlier. I tested the following out, and it seems to work like you want it to (passing "id=blahblah").

    Code:
    rewriteCond %{QUERY_STRING} ^([^/]+)
    RewriteRule ^reviews.php$ /review.php?val=%1 [R]
    Try that and let me know how it turns out for you ([R] to [L] if you want to mask the actual passage of the query string).

    Good luck!

  11. #11
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    Thanks, much better now but now i got trouble with URLs w/o GET string. It says 404.

    Right now i'm using this pattern:

    Code:
     RewriteCond %{QUERY_STRING} ^([^/]+)
     RewriteRule ^(test)/([^\?]+)$ /$2?store_name=$1&%1 [L]
    if i'm changing it to

    Code:
     RewriteRule ^(test)/?$ /index.php?store_name=$1 [R]
     RewriteCond %{QUERY_STRING} ^([^/]+)
     RewriteRule ^(test)/([^\?]+)$ /$2?store_name=$1&%1 [R]
    everything working ok but i don't want redirect [R]. i need test to remain in address bar.

  12. #12
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    Seems like following pattern works for me:

    Code:
     RewriteCond %{QUERY_STRING} ^([^/]+)
     RewriteRule ^(test)/([^?]+)$ /$2?store_name=$1&%1 [L]
     RewriteRule ^(test)/([^?]+)$ /$2?store_name=$1 [L]
    First rule handles requests with GET vars, second - w/o

  13. #13
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    Another question. If I'm using following pattern:

    Code:
     RewriteCond %{QUERY_STRING} ^([^/]+)
     RewriteRule ^([^/]+)/([^?]+)$ /$2?store_name=$1&%1 [L]
     RewriteRule ^([^/]+)/([^?]+)$ /$2?store_name=$1 [L]
    where ([^/]+) is matching first directory and i want some real directories like images, admin etc. to be excluded from that rule. Is it possible to make with RewriteCond or do I have to add some rules above?

  14. #14
    ABW Ambassador MoneyBusiness's Avatar
    Join Date
    March 14th, 2006
    Posts
    2,051
    Just a couple quick answers (sorry, I'm just waking up and the coffee hasn't kicked in yet):

    FIRST: Easy answer may be setting directory permissions through your server's control panel for the images directory, and the other one. That might override any other settings (possibly, i'm not sure about that).

    SECOND::

    The way .htaccess works, whenever a line is needed, the execution is pretty straightforward, and goes from top down. If the system encounters a line at the top, that's relative to the URL that's entered, it will execute that one first.

    Having said that, you could possibly enter something like the following (above your current entries):

    RewriteRule ^images/([^?]+)$ /noaccess.html [L]

    Does that make sense? If you put an entry like that above your current rewrite rules and conditions, it will execute first, and allow you to redirect traffic from the images directory, to a file you create called noaccess.html (or to the custom 404 error pages), before it even reaches the rest of your .htaccess entries.

    Good luck!

  15. #15
    ABW Ambassador sjangro's Avatar
    Join Date
    January 18th, 2005
    Location
    Boston
    Posts
    1,529
    Quote Originally Posted by SeymourButts
    Can you combine a rewriterule and a $_GET variable on the same page? I can't get it to work atleast.

    Here is a sample of the current ReWriteRule I am using....
    Code:
    RewriteRule   ^crap/([A-Za-z0-9-]+)/?$ crap.php?c=$1 [L]
    For example, when the user goes to....
    www . mysite . com/crap/LARGE/ the page is actually generated from crap .php?c=100

    Now, it is possible to then pass and read a $_GET variable from the rewritten page?

    Something like....
    www . mysite . com/crap/LARGE/?x=10

    Seems like a big mess.
    This thread has gone through a number of different methods, but I think maybe the original question hasn't fully been answered, or at least not the way I do it, which is actually quite simple.

    To reiterate.. you want the querystring parameters coming from the pretty url:

    /crap/large/?x=10

    to pass through to the rewritten url automatically, like this:

    /crap.php?c=large&x=10

    That's easily done using this rule:

    Code:
    RewriteRule ^crap/(.*)/$ crap.php?c=$1 [QSA,L]
    The trick is adding in the QSA to the rule. It stands for QueryString(somethng) and does just what you're talking about.

    Test it out using this simple script, crap.php:

    Code:
    foreach($_GET as $name => $value) {
    	echo "$name = " . $value . "<br>";
    }
    Where this URL /crap/large/ produces this output:

    Code:
    c = large
    And this URL /crap/large/?a=1&b=2&x=3&y=4&z=abestweb
    produces:

    Code:
    c = large
    a = 1
    b = 2
    x = 3
    y = 4
    z = abestweb
    Discovery of this QSA thing changed my life.

  16. #16
    Newbie
    Join Date
    October 11th, 2007
    Posts
    6
    Yahoo!!

    I solved the problem just by putting .htaccess containing

    Code:
    <IfModule mod_rewrite.c>
    
     RewriteEngine off
    
    </IfModule>
    into images folder.

    Quote Originally Posted by MoneyBusiness
    Just a couple quick answers (sorry, I'm just waking up and the coffee hasn't kicked in yet):

    FIRST: Easy answer may be setting directory permissions through your server's control panel for the images directory, and the other one. That might override any other settings (possibly, i'm not sure about that).

    SECOND::

    The way .htaccess works, whenever a line is needed, the execution is pretty straightforward, and goes from top down. If the system encounters a line at the top, that's relative to the URL that's entered, it will execute that one first.

    Having said that, you could possibly enter something like the following (above your current entries):

    RewriteRule ^images/([^?]+)$ /noaccess.html [L]

    Does that make sense? If you put an entry like that above your current rewrite rules and conditions, it will execute first, and allow you to redirect traffic from the images directory, to a file you create called noaccess.html (or to the custom 404 error pages), before it even reaches the rest of your .htaccess entries.

    Good luck!

  17. #17
    Yup, Sure ... now let me check ... Cagles Mill's Avatar
    Join Date
    January 18th, 2005
    Location
    The Midwest
    Posts
    646
    I keep trying this and I can't get it. Of course this is also the first time I seriously tried to figure it out.

    How would you write the Mod-Rewrite rule to convert:
    www . mysite.com/subdirectory/whatever.php?A=two-words&B=aNumber&C=aNumber

    to: www . mysite.com/two-words/B/C/ ?

    All my redirects would be following the same pattern except the third variable ('C') would be included in the original url only about half of the time.
    Rick M.
    I would rather have a bottle in front of me, than have a frontal lobotomy!
    Does your bubblegum lose its flavor on the bedpost overnight?

  18. #18
    Full Member
    Join Date
    March 10th, 2006
    Posts
    466
    Quote Originally Posted by Cagles Mill
    How would you write the Mod-Rewrite rule to convert:
    www . mysite.com/subdirectory/whatever.php?A=two-words&B=aNumber&C=aNumber

    to: www . mysite.com/two-words/B/C/ ?

    All my redirects would be following the same pattern except the third variable ('C') would be included in the original url only about half of the time.
    Well, first of all it looks to me like you are mixing up variable names with variable values:
    Variable names are: A,B,and C
    Variable Values are: two-words, aNumber, aNumber

    Variable values usually end up as directory names, e.g. "www . mysite.com/two-words/aNumber/aNumber/" and not "www . mysite.com/A/B/C/"

    In the example above you have mixed up the names and values. If you haven't already, you need to read this thread and understand post named "Part 4b: An Introduction to Mod_Rewrite as I know it." It's a great explanation for people seriously trying to figure stuff out.
    Last edited by mr_jones; October 24th, 2007 at 02:36 PM. Reason: Clarification

  19. #19
    Full Member
    Join Date
    March 10th, 2006
    Posts
    466
    Quote Originally Posted by Cagles Mill
    How would you write the Mod-Rewrite rule to convert:
    www . mysite.com/subdirectory/whatever.php?A=two-words&B=aNumber&C=aNumber

    to: www . mysite.com/two-words/B/C/ ?
    Oh, and one more thing, when I've done rewrites in the past I'm going from a url, e.g. mysite/dir1/dir2/page.html to a php script, e.g mysite/whatever.php?A=dir1&B=dir2&C=page. This is the opposite to your example and may be a source of mis-understanding.

  20. #20
    Yup, Sure ... now let me check ... Cagles Mill's Avatar
    Join Date
    January 18th, 2005
    Location
    The Midwest
    Posts
    646
    I'm not sure why, but I still just don't get it!

    Mr_jones, your suggestions helped. I had looked through that thread earlier, but without knowing what I was after, I couldn't find anything. Knowing to look at "Part 4b: An Introduction to Mod_Rewrite as I know it.", really helped. So I now understand the whole structure thing better. However, as suggested in the last post above, I think I am getting confused by which direction the code is being changed.

    So... here is a corrected example. I have a php file generating links such as:
    www . mysite.com/subdirectory/whatever.php?A=two-words&B=aNumber&C=aNumber

    I want to convert it to www . mysite.com/two-words/aNumber/aNumber.html

    The closest I can come up with (and which clearly does not work) is:
    RewriteRule ^subdirectory/whatever.php?([^/]+)([^/]+)([^/]+)$ two-words/aNumber/aNumber.html [L]
    Rick M.
    I would rather have a bottle in front of me, than have a frontal lobotomy!
    Does your bubblegum lose its flavor on the bedpost overnight?

  21. #21
    Full Member
    Join Date
    March 10th, 2006
    Posts
    466
    RewriteRule ^subdirectory/whatever.php?([^/]+)([^/]+)([^/]+)$ two-words/aNumber/aNumber.html [L]
    That one is pretty close, but you need forward slashes between the wild cards, and a “.html$” at the end.

    I’m assuming you want to end up on a page with a URL in the browser address bar as follows: www . mysite.com/subdirectory/two-words/aNumber/aNumber.html

    So you will have clicked on a link with the URL being: www . mysite.com/subdirectory/two-words/aNumber/aNumber.html. The Mod Rewrite will rewrite the URL to www . mysite.com/ whatever.php?A=two-words&B=aNumber&C=aNumber if you use a rewrite string in your htaccess file which looks like:

    RewriteRule ^subdirectory/([^/]+)/([^/]+)/([^/]+).html$ /whatever.php? A=$1&B=$2&C=$3[L]

    Where $1, $2, $3 are placeholders for the transferring variable values.

    The “[L]” at the end locks the originating URL, i.e. the URL before the rewrite. To trouble shoot the rewrite use “[R]”. Using [R] will give you a URL in your address bar looking like: www . mysite.com/subdirectory/whatever.php?A=two-words&B=aNumber&C=aNumber

    So, I think your page link address needs to be: www . mysite.com/subdirectory/two-words/aNumber/aNumber.html

    Then the rewrite will turn it into : www . mysite.com/whatever.php?A=two-words&B=aNumber&C=aNumber behind the scenes but display www . mysite.com/subdirectory/two-words/aNumber/aNumber.html in the browser bar.
    Last edited by mr_jones; October 24th, 2007 at 07:13 PM. Reason: Corrected to whatever.php?A=$1&B=$2&C=$3

  22. #22
    Yup, Sure ... now let me check ... Cagles Mill's Avatar
    Join Date
    January 18th, 2005
    Location
    The Midwest
    Posts
    646
    Thank you everyone! Especially mr_jones.

    I finally got it working. I think a large part of my problem is I did not understand that the clickable link must be the same as what you want it to appear as in the address bar. It makes sense in retrospect, but I didn't think you could create the pages dynamically unless the links themselves were standard PHP type links.

    So thanks again. I'll make a lot of use of this stuff.
    Rick M.
    I would rather have a bottle in front of me, than have a frontal lobotomy!
    Does your bubblegum lose its flavor on the bedpost overnight?

  23. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. How to compare the value of 2 string variables?
    By JayUSA in forum WebMerge (Fourthworld.com)
    Replies: 2
    Last Post: May 31st, 2007, 09:18 PM
  2. Affiliate cookies and URL variables
    By FREEDOM in forum Programming / Datafeeds / Tools
    Replies: 0
    Last Post: September 4th, 2006, 08:33 AM
  3. troubles with the $_GET['id'] funtion
    By fish4017 in forum Midnight Cafe'
    Replies: 12
    Last Post: April 28th, 2005, 07:17 PM
  4. variables as directories in url
    By markschok in forum Programming / Datafeeds / Tools
    Replies: 2
    Last Post: February 10th, 2004, 06:46 PM
  5. FLASH + variables?
    By c4 in forum Programming / Datafeeds / Tools
    Replies: 0
    Last Post: September 28th, 2002, 11:48 AM

Posting Permissions

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