treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Pretty URL problem

  • Using the following .htaccess I could go to site.com/about/ and that would work fine, but site.com/about (without the last / ) would not work.

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)/$ ./index.php?p=$1

    ErrorDocument 404 /404/

    But doing this won't work either
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)/$ ./index.php?p=$1
    RewriteRule ^(.*)$ ./index.php?p=$1

    ErrorDocument 404 /404/


    Anyone know how I can solve this? Thanks
  • If I understand this http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ correctly then I think it should work likes this:
        Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)$ ./index.php?p=$1

    ErrorDocument 404 /404/
  • Yeh, that's where I got the idea off of lol. That's fine, site.com/about will work, but not site.com/about/. It's very weird
  • Very strange. :?
    That post actually states:
    The asterisk means the user can enter whatever they want, and it won't affect the rewrite, this is primarily to handle a trailing slash so example.com/users/123 is the same as example.com/users/123/ as users would expect.

    If I get a bit more time later I will hit google with some well chosen search terms.
  • Ok, it started chucking down with rain just as I was about to leave, so I've been messing about with this for half an hour.
    After searching "htaccess pretty urls trailing slash" and mooching around the apache site it seems that the best option is to force (or remove) the trailing slash, so adding something like this to your first example should do the trick:
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://example.org/$1/ [R=301,L]

    Obviously replacing "example.org" with your domain.
    Based on this post http://soulpass.com/2008/01/11/htaccess-add-trailing-slash-to-url/
  • Thanks so much, but it still doesn't work. I understand why your solution should work, but mine does not. I looked at the page you referred from and I looked at the fist option of making both trailing slash and no trailing slash work. I figure its better to see why this doesn't work before forcing the /. It works because the trailing slash has the REGEX optional character ? stating that the trailing slash is optional, yet it doesn't work.

    I've attached a much striped down version of the site I use so you don't have to set up your own PHP engine for managing pages. Thanks for your help. I am totally confused. The site (a web app) will eventually help all designers and developers.
  • Sorry but I can't get this site to work at all.
    Notice: Undefined index: p in /Applications/MAMP/htdocs/htaccesshelp/index.php on line 3
    home

    Tried using isset but that just gave me a blank page.
  • Bizarrely, it does work on a live server, but not locally. However, the .htaccess doesn't seem to be doing anything at all.
  • Yeh, it's weird. I don't understand .htaccess. It's like you spend all this time learning and then its a waste when, probably, such a small thing is not working, and it could be probably be so simple, nobody covers it in their tuts. Arrgh, help please ;)