Forums

  • # 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 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 10:15 pm

    Awesome. Thanks

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

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag