Grow your CSS skills. Land your dream job.

htaccess gurus?

  • # July 6, 2013 at 3:29 pm

    Been playing around with htaccess for a while but I find it a bit of head melter most of the time.

    Currently I have a site with a few normal files such as index.php in the root folder. I also have a simple blog and each post is simple saved as a separate page at the moment in the form of article-name.php. My current htaccess file cleans the URLs to remove the .php extension etc.

    I’m attempting to move away from separate pages for each post to a single article.php file that will load the appropriate file directly out of a database I’ve set up to contain the posts.

    So the actual link is now article.php?title=article-name and obviously I’d prefer to tidy it to be simply example.com/article-name as it is now. Any ideas how to achieve this?

    Current htaccess looks like this:

    AddType text/x-component .htc
    RewriteEngine On
    RewriteBase /

    # remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{THE_REQUEST} ^GET (.*).php HTTP
    RewriteRule (.*).php$ $1 [R=301]

    # remove index
    RewriteRule (.*)/index$ $1/ [R=301]

    # remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]

    # add .php to access file, but don’t redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1.php [L]

    I’ve seen the following code given as an example of how to make it work and it does seem to do but with side effects

    RewriteRule ^([A-Za-z0-9-]+)/?$ article.php?title=$1 [NC,L]

    The issue is of course that it tries to rewrite index.php. Also if you navigate to example.com/folder it’ll mess around with that too.

    Any help appreciated

    # July 6, 2013 at 10:58 pm

    >My current htaccess file cleans the URLs to remove the .php extension etc.

    This is a complete waste of time and effort. If the user types in a URL that your site can understand, **let them**. If you want your site to have “pretty urls,” **you** should change all of your hyperlinks from `example.com/ugly.php?this=that-or-something` to `example.com/pretty-url` – htaccess is _not_ the right tool for this job.

    mod_rewrite is intended to take URLs that _are not_ usable and _make_ them usable. Your current htaccess takes a url that is entirely usable and goes through 1-4 hard redirects to make it pretty, just so it can rewrite it _again_ and return it to its original format…! Up to four requests for _a single page!_ (Plus, all the other confusion you’re running into!)

    > RewriteRule ^([A-Za-z0-9-]+)/?$ article.php?title=$1 [NC,L]

    **This** is the example you should be following.* Take the “pretty” url and rewrite it behind-the-scenes so your server can make sense of it.

    * well, almost: the dash in the regex should either be escaped or (preferably) at the beginning of the character class: `[-A-Za-z0-9]+`

    My suggestion would be to start by commenting out your existing htaccess code. What do you want to do with your other files? Do you ever need to access them directly? Do you want them to have pretty URLs also?

    # July 8, 2013 at 2:03 pm

    RewriteRule ^archive/?$ archive.php [NC,L]
    RewriteRule ^([-A-Za-z0-9]+)/?$ article.php?link=$1 [NC,L]

    All links are already listed as “example.com/whatever-page”. The code above seems to work fine.

    The only issue now is that I have some folders “example.com/folder” that should be accessible but don’t work unless you type the full address “example.com/folder/index.php”

    Thanks for any help

    # July 8, 2013 at 9:31 pm

    If you want URLs that point at directories to go to their index.php page (correct?), you can do this (put it _before_ your other rewrite rules):

    # if requested filename is a directory
    RewriteCond %{REQUEST_FILENAME} -d

    # add “index.php” to the end
    # ( “L” means “last rewrite.” )
    RewriteRule ^(.*)$ $1/index.php [L]

    # July 8, 2013 at 10:15 pm

    Awesome. Thanks

    # July 8, 2013 at 11:47 pm

    welcome!

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

You must be logged in to reply to this topic.

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