Grow your CSS skills. Land your dream job.

.htaccess custom url with php

  • # June 25, 2009 at 2:47 pm

    Similar to WordPress permalinks. Does anybody know how to make a .htaccess file that imitates an url.

    An example is a site I am building. Pages are selected by going to http://www.doamin.com/d/?p=about

    When users go to http://www.doamin.com/d/about/ I would like the browser to read http://www.doamin.com/d/about/ but use http://www.doamin.com/d/?p=about

    I know this can be done, but have absolutely no idea how to combine .htaccess with php.

    I have googled to no avail. I can’t think of any good keywords to use, nothing to describe the issue here.

    # June 25, 2009 at 3:08 pm

    I just searched "htaccess pretty urls" and came up with a whole bunch of results. Here’s a nice one that seems to make sense even to me with my rudimentary knowledge of PHP.

    # June 25, 2009 at 3:56 pm

    thanks, I looked at alot of the links using ur search suggestion, but is it me or are there tuts just far too confusing.

    Well they are to me :cry:

    If somebody could just explain in simple terms I would be very greatfull

    # June 25, 2009 at 4:02 pm

    Sorry, I forgot to include the link. :oops:
    http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

    # June 25, 2009 at 5:29 pm

    It is a confusing subject because it involves two of the great mysteries of the intarwebz. Apache .htaccess files and regular expressions.

    There is no way around it. Regular expressions are hard. I found them hard when I first encountered them, and I sometimes still use a cheatsheet when creating them. But don’t let that put you off. Learn them. Regular expressions are found not just in .htaccess, they are all over the place – PHP, Perl, Shell scripts, Ruby on Rails, Javascript, Super Deluxe Automatic Coffee Makers. Alright, maybe not that last one but they are pretty much everywhere else and that makes them worth learning!

    Apostrophe posted a good link about .htaccess so I will post one about regular expressions in general – http://www.regular-expressions.info/

    # June 25, 2009 at 6:00 pm

    cool I’ve heard of RE before but didn’t look to much into it. i’m getting confused. I’ve tried this but it doesn’t work, any ideas?

    Code:
    RewriteEngine on
    RewriteRule ^d/([a-z]*)/$ /d/index.php?p=$1
    # June 26, 2009 at 4:46 pm

    In the d/ folder, add this:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) index.php?p=$1 [L]
    # June 27, 2009 at 8:36 am

    Thanks, that worked, to a certain extent.

    It delivers the page. But the stylesheet won’t link, and the includes() function doesn’t work.

    # June 29, 2009 at 8:50 am

    put a slash for every stylesheet, img and include. It should work then.

    Code:
    “includes/stylesheet.css”

    “/includes/stylesheet.css”

    # June 29, 2009 at 11:40 am

    I recommend anyone that wants to achieve clean urls and is starting a site from scratch should look at using a MVC/MTV framework

    CodeIgniter, cakePHP, Zend Framework ect…

    Keep in-mind the way they handle the rewrite rule breaks the way most people are accustomed to building web sites….

    Traditionally sites follow a folder structure
    Example:

    Code:
    http://example.com/home/index.php
    folder “home” -> file “index.php”

    The Rewrite will instead of looking in that folder for that file it treats everything a variable

    http://example.com/home/index

    Rewrites to http://example.com/index.php

    Then index.php takes "home/index" as a variable and delivers content accordingly
    (Controller = ‘home’, Action = ‘index’)

    In addition, this will allow you to define custom uri structures like

    Code:
    $customURI = ‘profile/:userName';

    http://example.com/profile/benjamin

    This will point to the profile controller and give a username variable equal to benjamin allowing you to pull up content about a unique person

    Anyways, take a look at it, alot of sites use frameworks like such and for good reason too, very powerful, extensive useful libs and faster development of your sites

    # June 29, 2009 at 3:34 pm

    thanks for the advice, but I want a light site with all my own coding. I need to learn how to do it all myself to improve my skills, no offense. But yeh, beginners or anyone for that matter could use frameworks to make the job easier.

    # January 19, 2010 at 7:31 pm

    I’m currently learning CodeIgniter and am having problems with the .htaccess regex part.

    From their wiki, I’m using the following RewriteRule:

    Code:
    RewriteRule ^(.*)$ index.php?/$1 [L]

    Yet it does not work.

    works http://60dayflip.com/index.php/site/dosomething
    doesn’t http://60dayflip.com/site/dosomething
    doesn’t http://60dayflip.com/dosomething

    Any clues?

    # January 19, 2010 at 8:13 pm

    Go to the config.php file and change

    Code:
    $config['index_page'] = “index.php”;

    to

    Code:
    $config['index_page'] = “”;
    # January 21, 2010 at 3:45 am

    Yep, that’s the first thing I did.

    # April 17, 2013 at 11:09 pm

    i think u must add base url in front of your function location and image location.

    Example : you put css like this …href=”css/main-style.css”

    it should be like this …href=”http://www.yourdomain.com/css/main-style.css”

    correct me if i’m wrong :)

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

You must be logged in to reply to this topic.

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