Results 1 to 15 of 15
  1. #1
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    How do you make fancy/pretty URLs in a PHP script?
    Hi, I'd like to turn the "get" variables to show up as a normal URL, but I don't want to set up a huge .htaccess file.


    How can I change #1 to #2 (or #3) in a php script?

    #1 http://mydomain.com/myscript.php?bla...ep=abc&boom=on

    #2 http://mydomain.com/myscript/324/abc/on

    #3 http://mydomain.com/myscript/blah/324/eep/abc/boom/on

    Just in case that isn't clear. I want the variables to be in the url and then interpreted in php back into variables for use in the script. I'd therefore be able to rewrite links to "self" using pretty links instead of normal ?blah=324&eep=abc

  2. #2
    Fear and Arrogance jrrl's Avatar
    Join Date
    January 18th, 2005
    Location
    Pittsburgh
    Posts
    485
    You can get #2 by adding something like this to the top of your scripts:
    PHP Code:
    list($dummy,$_GET['var1'],$_GET['var2']) = split('/'$_SERVER['PATH_INFO']); 
    where var1, var2, etc. are the names you would have used after the '?' in the ugly version.

    You need the $dummy, because PATH_INFO starts with a slash.

    Of course, you still have to change your urls when your script produces them.

    Hope this helps.

    -John.
    There's a reason army's wear uniforms even though it makes them easier to spot. Sometimes that's what you want. Uniforms suggest organization, power, and numbers. These, in turn, inspire fear. And, as any good operative knows, there is no more effective weapon than fear.

    Hosting Comparison - HostScope - jrrl.com

  3. #3
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    Quote Originally Posted by lostdeviant
    Hi, I'd like to turn the "get" variables to show up as a normal URL, but I don't want to set up a huge .htaccess file.
    I would let myscript.php live in it's own directory and not root. Then you could put a little .htaccess file with mod_rewrite in that directory and it would be small and just for your script. Not a site wide .htaccess like the one that normally is in the root directory. All my sites are like that and it works for me.


  4. #4
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    I think this is what I need. Thank you

    Quote Originally Posted by jrrl
    You can get #2 by adding something like this to the top of your scripts:
    PHP Code:
    list($dummy,$_GET['var1'],$_GET['var2']) = split('/'$_SERVER['PATH_INFO']); 
    where var1, var2, etc. are the names you would have used after the '?' in the ugly version.

    You need the $dummy, because PATH_INFO starts with a slash.

    Of course, you still have to change your urls when your script produces them.

    Hope this helps.

    -John.

  5. #5
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    Doing that got the queries in a normal structure, but with http://mydomain.com/myscript.php/324/abc/on

    Unfortunately I couldn't remove the .php from the url. It would be ideal to be able to do that without having to touch the .htaccess file. I'm going to do some Google searches right now.

  6. #6
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    It would be ideal to be able to do that without having to touch the .htaccess file.
    What is your reason for not wanting to use .htaccess? Just curious.


  7. #7
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    .htaccess would make it more complicated. IMHO
    I suppose I'll have to use it to get rid of the .php.

  8. #8
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    I started with post #44 in Gardner's famous Bullet Proof thread and went from there.


  9. #9
    Newbie
    Join Date
    June 3rd, 2007
    Location
    the Netherlands
    Posts
    46
    Quote Originally Posted by lostdeviant
    .htaccess would make it more complicated. IMHO
    I suppose I'll have to use it to get rid of the .php.
    I don't get it why would these 3 lines in your htaccess makes things complicated?

    RewriteEngine on
    #RewriteBase /
    RewriteRule ^(.*)/(.*).php product.php?cat=$1&keyword=$2


    next what happens in this example: You have a url that looks like this http://www.myurl.com/1234/theproduct.php
    this is translated by htaccess and the file product.php is called with 2 variables. First the variable $cat which has a value 1234 in this case and $keyword which has a value of theproduct

    You can change it anyway you want make it more simpler or even more complicated but the basis is imho much easier then trying to create a script that mimics this behavier

  10. #10
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    I have quite a few variables to add and I'm worried that any rewrites could mess up Wordpress's rewrite rules.

    I got it working pretty well using path info except I still have .php in the url.

    I would like to know a rewrite to just not show the .php and then let my script take care of the variables if that is possible.

  11. #11
    Fear and Arrogance jrrl's Avatar
    Join Date
    January 18th, 2005
    Location
    Pittsburgh
    Posts
    485
    Quote Originally Posted by lostdeviant
    I have quite a few variables to add and I'm worried that any rewrites could mess up Wordpress's rewrite rules.

    I got it working pretty well using path info except I still have .php in the url.

    I would like to know a rewrite to just not show the .php and then let my script take care of the variables if that is possible.
    Ah. I think I can help here. I do the same thing (well not the variables thing, but the no .php thing) with a PHP script of mine called bounce. I add this to .htaccess and all is goodness and light:

    <Location /bounce>
    ForceType application/x-httpd-php
    </Location>

    Obviously, you would have to change the Location thing to match your script's path, but voila! no .php and yet it gets treated as php by the server.

    -John.
    There's a reason army's wear uniforms even though it makes them easier to spot. Sometimes that's what you want. Uniforms suggest organization, power, and numbers. These, in turn, inspire fear. And, as any good operative knows, there is no more effective weapon than fear.

    Hosting Comparison - HostScope - jrrl.com

  12. #12
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    Doesn't that mean I'll have to rename my php files?

    Quote Originally Posted by jrrl
    Ah. I think I can help here. I do the same thing (well not the variables thing, but the no .php thing) with a PHP script of mine called bounce. I add this to .htaccess and all is goodness and light:

    <Location /bounce>
    ForceType application/x-httpd-php
    </Location>

    Obviously, you would have to change the Location thing to match your script's path, but voila! no .php and yet it gets treated as php by the server.

    -John.

  13. #13
    Fear and Arrogance jrrl's Avatar
    Join Date
    January 18th, 2005
    Location
    Pittsburgh
    Posts
    485
    Quote Originally Posted by lostdeviant
    Doesn't that mean I'll have to rename my php files?
    Um, yeah. Wasn't really thinking about that part. I suppose you could symlink rawname to phpname but I don't know a lot of hosting control panels that let you do that.

    There is a mod_rewrite way to do it... let me play for a few minutes and see what I can come up with...

    -John.
    There's a reason army's wear uniforms even though it makes them easier to spot. Sometimes that's what you want. Uniforms suggest organization, power, and numbers. These, in turn, inspire fear. And, as any good operative knows, there is no more effective weapon than fear.

    Hosting Comparison - HostScope - jrrl.com

  14. #14
    Visual Artist & ABW Ambassador lostdeviant's Avatar
    Join Date
    September 7th, 2007
    Location
    Cuautitlán, Edo. de México
    Posts
    1,725
    Thanks. I just wanted to make sure I understood you. I guess it isn't a big deal to rename the file :-)

  15. #15
    Newbie
    Join Date
    June 3rd, 2007
    Location
    the Netherlands
    Posts
    46
    Quote Originally Posted by lostdeviant
    I have quite a few variables to add and I'm worried that any rewrites could mess up Wordpress's rewrite rules.

    I got it working pretty well using path info except I still have .php in the url.

    I would like to know a rewrite to just not show the .php and then let my script take care of the variables if that is possible.
    It was just an example !! Change the lines i gave you too anything you like fi
    http://yoururlisthis.com/12345/nicem...tegory/product

    RewriteEngine on
    #RewriteBase /
    RewriteRule ^(.*)/(.*)/(.*)/(.*) product.php?variable1=$1&variable2=$2&variable3=$3&variable4=$4

    So that url is translated into
    Variable1 = 12345
    variable2 = nicemaincategory
    variable3 = subcategory
    variable4= product

  16. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. need help with php script
    By oscar in forum Programming / Datafeeds / Tools
    Replies: 15
    Last Post: June 29th, 2004, 05:15 PM

Posting Permissions

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