Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Apache mod_rewrite

  • 17-04-2009 11:03am
    #1
    Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭


    I am hoping somebody who is experienced with apache mod_rewrite can help me out with something.

    Just working on a little blog script at the moment and seem to be hitting a wall with regards to getting "friendly URLs" for the articles. The site it is being implemented on uses mod_rewrite to remove the ".php" from page names (so /contact over /contact.php).

    When we try to alter the blog (at present its just storing the entrys in MySQL and pulling them by ID) to allow for cleaner names for the article it causes the website to stop working as it seems to conflict with the .htaccess.

    Any suggestions on a fix?


Comments

  • Closed Accounts Posts: 1,200 ✭✭✭louie


    can you show us what you have in the .htaccess file?

    Is this a wordpress blog or created by yourself?


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Sorry for the delay.

    The current .htaccess can be viewed here with the changes we were adding from here.

    Its a custom blog, developed in house and not by WordPress or whoever. :)


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭tomED


    It sounds like you are forgeting one fundemental issue... and that is that you are referencing each item by an ID.

    So in effect you need to carry that ID in the url. For example:

    /myarticlename/12/ or /12/myarticlename/

    The htaccess file doesn't seem to be correctly set up to what you want to do either.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    tomED wrote: »
    It sounds like you are forgeting one fundemental issue... and that is that you are referencing each item by an ID.

    So in effect you need to carry that ID in the url. For example:

    /myarticlename/12/ or /12/myarticlename/

    Thats not the issue at hand.
    The htaccess file doesn't seem to be correctly set up to what you want to do either.

    As I said, the .htaccess is only covering the removal of the .php. The second url covers the changes we were trying to make to allow friendly urls, which then caused the site to stop working.

    It seems that neither will work together with our code base.


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭tomED


    Ok Sully

    Well you'll have to give better examples and explain a little more.

    You won't be able to run both side by side - you'll have to run one or the other (if they are in the same directory).

    Tom


  • Advertisement
  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    tomED wrote: »
    Ok Sully

    Well you'll have to give better examples and explain a little more.

    You won't be able to run both side by side - you'll have to run one or the other (if they are in the same directory).

    Tom

    Okay, well ill try my best to be more specific.

    Initally, apache mod_rewrite was being used to remove the ".php" from page names. So if you wanted to visit a page called contact.php it removed the .php (eg http://www.example.com/contact/ instead off http://www.example.com/contact.php). This is what the .htaccess I have linked in an earlier post covers. It has the code to strip away the .php which is working perfectly.

    The custom blog development is going to use "friendly urls" like a lot of decent blogs out there (eg http://www.example.com/blog-article-title-here/ over http://www.example.com/blog?id=XX). In order to set this up it needs to be implemented into the .htaccess, which is where the second link comes in. The changes were being based on that. When the changes were implemented, the site was no longer accessible.

    The assumption is that the new system conflicts with the old. The question is, how can one have both?

    This is an area id be pretty rough in so looking to see if anybody with the experience in it can help (so far, its becoming increasingly difficult to find someone who can).


  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭tomED


    In a nutshell, you will have great difficulty doing this. In fact, because of what you are doing with the ".php" replacment, I would even go as far as to say it's impossible to do both here.

    Your best bet is to change your structure to something like /blog/nameofarticle and use a different htaccess file in the /blog/ directory.

    Tom


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Thanks for the suggestion. Will give it a whirl and see what happens but from what I gather, it may still not be possible. Will try it though and let you know how it goes :)

    Thanks for your help, appreciated.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    This has been fixed, thanks for your assistance. A friend of mine across the water is a developer so he helped me out on addressing this tonight. The blog didnt need be in a seperate directory and worked fine with the current setup.

    The problem was the code we added to the .htaccess was just slightly incorrect and resulted in errors. Just some minor tweaks and it was working perfectly. We now have the site running as it was, with a blog using friendly URLs in the root. :)

    Thanks again


  • Subscribers Posts: 9,716 ✭✭✭CuLT


    Was pointed to this nifty looking htaccess generator recently.

    Will probably give it a whirl next time I have to do something with htaccess I'm not familiar with, but you might find it useful :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,452 ✭✭✭tomED


    Sully wrote: »
    This has been fixed, thanks for your assistance. A friend of mine across the water is a developer so he helped me out on addressing this tonight. The blog didnt need be in a seperate directory and worked fine with the current setup.

    The problem was the code we added to the .htaccess was just slightly incorrect and resulted in errors. Just some minor tweaks and it was working perfectly. We now have the site running as it was, with a blog using friendly URLs in the root. :)

    Thanks again

    Excellent - any chance of seeing the finalised htaccess file for future reference?


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    CuLT wrote: »
    Was pointed to this nifty looking htaccess generator recently.

    Will probably give it a whirl next time I have to do something with htaccess I'm not familiar with, but you might find it useful :)

    Thanks for that, will keep it handy.
    tomED wrote: »
    Excellent - any chance of seeing the finalised htaccess file for future reference?
    RewriteEngine On
    RewriteBase /
    
    
    RewriteRule ^blog/(.*) blog.php?name=$1 [L]
    
    # 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]
    

    Its just a one line addition below the ReWriteBase.


Advertisement