Results 1 to 33 of 33
  1. #1
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88

    // Short URLs with PHP?

    Hi everyone!
    I'm sorry if it's been mentioned before (althought I searched through Programming forum and haven't found anything), but is there a way to make URLs shorter?
    For instance, I have an url like "index.php?category=books&subcategory=fiction", and I want something like "siteurl/books/fiction".
    I've read about an Apache module, but I don't think I have access to my hoster's httpd file.
    Is there a way to do it in PHP?

    Thanks a lot!
    Last edited by ACIDman; 03-19-2004 at 10:50 PM.

  2. #2
    I think it could be done in the .htaccess file.

    Someone confirm this though.

  3. #3
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    indeed it can

    if you do an

    AddHandler my-file-type *
    Action my-file-type index.php

    in the .htaccess file the requests will be given to index.php (not sure about the wildcard format you may have to use AddHandler . instead).

    In order to get the path you will need to use the global $_SERVER.

  4. #4
    I'm interested in knowing how to do this, also. I'm slightly new to what the advice that was given is? Sorry, I'm a newbie

  5. #5
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    I sort of got the idea but could you explain what do I do next? I add those lines "AddHandler" etc. to .htaccess, and then?
    And how will you use the $_SERVER variable (I thought you can only use it in PHP files)?
    Thanks .

  6. #6
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    I'm sorry... I'm alittle bit out of it at the moment, as can be seen by my other posts today.

    These directions are wrong. Give me 1/2 and I'll write a small how-to and put together an example on this.

  7. #7
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Thanks a lot, dude!

  8. #8
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Ok. Visit
    http://www.flaminglunchbox.net/artic...directPHP.html
    It's quick and dirty. Enjoy.

  9. #9
    Join Date
    Jul 2003
    Location
    NYC
    Posts
    245
    Nice...
    just what I needed to know..
    thanks dude!
    ---|| Avurt Inc. - ||---
    www.avurt.com
    Banners, Prints, Graphics, Web sites & Much more

  10. #10
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Yeah, it works .
    Thanks!

  11. #11
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Great! I can now go to sleep content that I've done my good deed for the day. If you have any comments post them here or send an email or im. I'll address them if I can.

  12. #12
    Join Date
    Jan 2003
    Location
    Perth, WA, Australia
    Posts
    1,276
    You can look into Apache's mod_rewrite, it's the most widely used method for doing this type of thing.

    Look it up on Google, you'll find a heap of tutorials/examples.
    nu-metal.org :: coming soon

  13. #13
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Yea, that's what I was talking about. Althought since I'm using my hosting for free I don't feel like demanding something from them . The PHP method above solves the problem (I think it's even easier than with rewrite).

  14. #14
    Join Date
    Jan 2003
    Location
    Perth, WA, Australia
    Posts
    1,276
    You can use mod_rewrite from within .htaccess files.
    nu-metal.org :: coming soon

  15. #15
    Join Date
    May 2002
    Location
    UK
    Posts
    2,994
    You don't have to use mod_rewrite for that. You can remove the file extension of the PHP file and using the <Files> directive in .htaccess force it to PHP.

    Then inside the PHP file you can work out the query string using environment variables.

  16. #16
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Exactly, though in most cases you don't even need to mess with a .htaccess file. Apache will negotiate the file extensions within itself. Check out the how-to I provided earlier in this thread.

  17. #17
    Join Date
    Feb 2002
    Posts
    3,727
    I think mod_rewrite can do this cleanly without a problem. If you however have found another method using .htaccess, good for you.
    Have you Floble'd today?

  18. #18
    I've used both the mod_rewrite method and forcing the handler (if you want to get rid of the .php extension, you do have to force the handler).

    In the end, I think mod_rewrite, at least in some cases, can be cleaner. And if you have a large site, it's a lot faster to implement.

    Here's a thread disussing the mod_rewrite method in detail:

    http://www.webhostingtalk.com/showth...ht=mod_rewrite

  19. #19
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    OIS. on my apache setup as well as every setup I have worked with so farI do not have to force the handler. The standard apache negotiation discovers the script just fine.

  20. #20
    Join Date
    Apr 2003
    Location
    Kenilworth, IL
    Posts
    672
    mod_rewrite does a fine job with this =]

  21. #21
    In the example you linked to, you had a URL like "/script.php/some/variables". This does work without a ForceType. If you want to have a URL that really looks like a directory structure, say "/script/some/variables", then you'll need to somehow tell apache to parse "script" as PHP. You would normally do this in .htaccess like this:

    Code:
    <Files script>
      ForceType application/x-httpd-php
    </Files>
    If you are able to parse files w/o an extension as PHP (and w/o using ForceType), I would be interested to know how you have httpd.conf set up.

    But, again, mod_rewrite actually gives a lot more flexibility.

  22. #22
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Two things. Firstly read all of the how-to. I provide a link at the end of the document on just this. Secondly you should probably try doing this on your own system. If you have mod_negotiation installed (which nearly all apache setups do) it will work just fine. I suggest that you read up on mod_negotiation to avoid being confused when your logs say files were being accessed from incomplete addresses.

  23. #23
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Hmm. I can't get mine to work. I have Apache 2.0, and uncommented the "modules/mod_rewrite.so" line in the httpd.conf and permited all instructions for .htaccess files. Now, I created the .htaccess with simple content -
    RewriteEngine On
    RewriteRule ^/testpage$ /TestFolder/index.php
    So, now when I type "localhost/test" it should display index.php from TestFolder, right? But it just says - "Page not found".
    How can I check whether mod_rewrite has loaded or not? It should've loaded the module..
    I checked if .htaccess works at all with DirectoryIndex - it does. So, the problem is either instructions (which are quite basic so I hope there's no error) or in mod_rewrite not getting loaded.
    What's wrong? ..
    Last edited by ACIDman; 03-21-2004 at 03:28 AM.

  24. #24
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Assuming mod_rewrite is loaded (and also that you are typing 'localhost/testpage' instead of 'localhost/test' you might want to try this:

    RewriteRule ^testpage[/]?$ /TestFolder/index.php [L]

    that will match the path name with or without the trailing '/'. Also, since you're doing this in a .htaccess you don't get the '/' at the beginning.

    Sadly, none of this will work if your host has not set AllowOverride FileInfo on the directories you're trying to do this in.

  25. #25
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    I'm trying this on localhost so I can change anything .
    I'll try what you advised in a minute..

    It works! . Now I know it works at all. Good .
    Last edited by ACIDman; 03-22-2004 at 01:50 AM.

  26. #26
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Now.. I'm having another problem .
    Let's say, these are my rules:

    RewriteEngine On
    RewriteRule ^([a-z]+)[/]?$ /index.php?category=$1

    So, when I type mysite/somecategory, I get the achieved result. Although, if I put the slash right at the end, I get the result too BUT all the imagess and CSS's paths are screwed up - Apache now looks in mysite/somecategory/images for the images.
    Same thing happens if I try to create rule like "mysite/category/subcategory".
    How do I fix it?

  27. #27
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Anyone? :/

  28. #28
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    In your html you refer to the images and css using a relative path don't you? What's happening is that your rewrite is making the browser think the page is coming from mysite/category when it's actually from mysite. Sans the '/' this is fine, as the browser assumes that mysite/category is a file instead of a directory. With the slash it thinks its a directory and looks for images in mysite/category/images instead of mysite/images.

    Either add a rewrite rule for the images too, or explicitly redirect the browser so that it knows what the real path is. You could also change the references created by the php to be absolute. I would probably go with the last one for what it sounds like you're doing.

  29. #29
    Join Date
    Jul 2003
    Location
    Vancouver, BC, Canada
    Posts
    88
    Oh, I could've realised it . Thanks!
    I'll just change my relative paths to direct ones, that's the easiest way I presume.

  30. #30
    I'm still confused.. I want to do this with mod rewrite but I don't understand any of the things going on.. it's confusing. Is there any tutorials or downloadable examples I can read about that do the "Clean URLs" ?

  31. #31
    I'm still confused on this.
    Last edited by inque; 03-24-2004 at 05:15 PM.

  32. #32
    Join Date
    Sep 2002
    Location
    Illinois
    Posts
    2,304
    Use mod_rewrite to make urls look better.

    PHP version is much slower and your access_log in Apache will be filled with 404 Requests
    How's my programming? Call 1-800-DEV-NULL

  33. #33
    Join Date
    Feb 2004
    Posts
    772
    Hi,

    The below URL will give you better picture of "short URL" :

    http://docs.xaraya.com/docs/rfcs/rfc...#rfc.section.1

    Hope this may resolve your problem.

    Regards,

    Bright

Posting Permissions

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