Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other htaccess gurus?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #46161
    AWKM
    Participant

    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

    #141933
    AWKM
    Participant

    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

    #141965
    AWKM
    Participant

    Awesome. Thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Other’ is closed to new topics and replies.