www.webdeveloper.com
Results 1 to 3 of 3

Thread: mod_rewrite in .htaccess is giving me a fight

  1. #1
    Join Date
    Oct 2007
    Posts
    126

    mod_rewrite in .htaccess is giving me a fight

    Hi I've got some problems getting my mod_rewrite to work in .htaccess. I'm trying to rewrite my dynamic URLs to more presentable links. Parts of it work but not as a whole. I have no experience of it before so I've used bits of code from tutorials and there is probably a more effective way of writing it.

    Anyway here is the problem. I have three levels of variables that govern the dynamic URLs, for instance

    1. I could have a link to the Services section:
    HTML Code:
    www.myurl.com/index.php?mainSection=Services
    and want to change it to: www.myurl.com/Services

    2. I could have a link to the Downloads within the Services section:
    HTML Code:
    www.myurl.com/index.php?mainSection=Services&subSection=Downloads
    and want to change it to: www.myurl.com/Services/Downloads
    3. I could have a link to the PDFs within Downloads within the Services section:
    HTML Code:
    www.myurl.com/index.php?mainSection=Services&subSection=Downloads&subsubSection=PDFs
    and want to change it to: www.myurl.com/Services/Downloads/PDFs
    And here is my .htaccess to make the above work

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    #RULE 1: this rule should handle the link to services
    RewriteRule ^([^/\.]+)/?$ index.php?mainSection=$1 [L]
    
    #RULE 2: this rule should handle the link to downloads within services
    rewriterule ^([^-]+)/([^&/]+) /index.php?mainSection=$1&subSection=$2 [L]
    
    #RULE 3: this rule should handle the link to PDFs within downloads within services
    rewriterule ^([^-]+)/([^&/]+)/([^?\&/]+) /index.php?mainSection=$1&subSection=$2&subsubSection=$3 [L]
    
    </IfModule>
    The problems that I've got is rules 1 and 2 work but rule 3 doesn't work and only works if I remove Rule 2.

    Also to make matters more confusing: some pages governed by rule 3 actually carry a question mark in the title such as; "Why should I download these documents?" - Rule 3 then doesn't work and when I test what's happening it seems that the '?' has been left off.

    Any help please?

    Thanks

  2. #2
    Join Date
    Jul 2009
    Location
    Tennessee
    Posts
    28
    Have you tried reordering your rewrite rules to go from most specific to least specific (reverse their order)? That should make all three rules work as expected...

    I think your question mark problem is due to the question mark in your third regular expression. Try removing the ? from your expression and see if that helps.

  3. #3
    Join Date
    Oct 2007
    Posts
    126
    Thanks Dave, both suggestions worked. I didn't really want to leave off the question mark out of the title as I felt that was not really solving the problem correctly, but lifes too short, so I used a str_replace to get rid.


    Thanks

    Chris

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles