Grow your CSS skills. Land your dream job.

Pretty URL problem

  • # August 11, 2009 at 8:19 pm

    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.

    Code:
    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

    Code:
    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

    # August 12, 2009 at 2:25 am

    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:

    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

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

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

    ErrorDocument 404 /404/

    # August 12, 2009 at 4:47 am

    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

    # August 12, 2009 at 5:05 am

    Very strange. :?
    That post actually states:

    Quote:
    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.

    # August 12, 2009 at 6:18 am

    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:

    Code:
    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/

    # August 12, 2009 at 9:52 am

    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.

    # August 12, 2009 at 10:58 am

    Sorry but I can’t get this site to work at all.

    Quote:
    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.

    # August 12, 2009 at 11:10 am

    Hum, I just uploaded the attached file onto my test domain.

    http://votsikas.com/htaccesshelp/

    It works.

    http://votsikas.com/htaccesshelp/index.php?p=about
    http://votsikas.com/htaccesshelp/index.php?p=contact
    http://votsikas.com/htaccesshelp/index.php?p=404
    http://votsikas.com/htaccesshelp/index. … 4f4f45tf5t [none exsistant, returns 404]

    Some weird things happening today

    # August 12, 2009 at 1:59 pm

    Bizarrely, it does work on a live server, but not locally. However, the .htaccess doesn’t seem to be doing anything at all.

    # August 12, 2009 at 2:26 pm

    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 ;)

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".