SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Redirecting with very similar URLs

    I've been asked to put some redirects in place on our server that will forward all of these to one location:


    • domain.com/item
    • domain.com/item/
    • domain.com/item.html
    • domain.com/item.html/
    • domain.com/category/item
    • domain.com/category/item/
    • domain.com/category/item.html/


    These should all go to domain.com/category/item.html however everything I try seems to end up in an infinite loop. Please help!

    This is what I have so far:

    Code:
    Options +FollowSymlinks
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.domin\.com
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^category/item?$ /category/item.html [L,R=301]
    RewriteRule ^category/trafalgar/?$ /category/item.html [L,R=301]
    RewriteRule ^category/trafalgar.html/?$ /category/item.html [L,R=301]
    RewriteRule ^item?$ /category/item.html [L,R=301]
    RewriteRule ^item/?$ /category/item.html [L,R=301]
    RewriteRule ^item.html?$ /category/item.html[L,R=301]
    RewriteRule ^item.html/?$ /category/item.html [L,R=301]
    The 301s are actually 302s in my tests so I don't permanently send myself to the wrong place. The problem appears to be that item always matches, irrespective of whether category preceeds it or not. I'm not entirely sure how to deal with that as it's never been a requirement before.

    Any help please?

  2. #2
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    I think (and I'm not an htaccess expert, but I like a challenge!) the problem is with your use of the question mark.

    Taken from http://www.yourhtmlsource.com/sitema...rewriting.html:
    The question mark means ‘zero or one of the preceding character or range’
    where (from their examples), the preceding character or range seems to be defined by being in brackets. I'm guessing that without brackets, it takes everything into account, so will be a universal match. If I've guessed right, what you actually want, instead of
    Code:
    RewriteRule ^item?$ /category/item.html [L,R=301]
    RewriteRule ^item/?$ /category/item.html [L,R=301]
    is
    Code:
    RewriteRule ^item(/)?$ /category/item.html [L,R=301]
    But I freely admit that is off the top of my head, and take no responsibility for any catastrophes that may ensue if you just copy and paste this without testing it...

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Stevie,

    Good try! The ? in regex is "zero or one of the preceding character" but the square brackets you referenced could specify the character as being in an allowable set, i.e., it has nothing to do with grouping. Your use of the parenthetical does group and, if followed by a ? will make the entire group optional.

    Antnee,

    Redirect (almost) everything to one location, eh? Why not:

    Code:
    RewriteEngine on
    
    RewriteRule ^(category/)?item(/|\.html/)?$ category/item.html [R=301,L]
    Okay, that looks a bit complex but it says:

    Start with an optional category/, continue with item, continue with EITHER an optional trailing / OR .html/ and ends. The redirection will not loop as category/item.html does not have a trailing /.

    WARNING, a redirection like this (many different levels of directories for your request) will force the use of (internal) absolute links in all support files (.css, .js, .gif, .jpg, etc.) and, IMHO, a trailing / is something which is ONLY done for a directory request.

    Regards

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Feeling a bit daft actually. I knew that about the ? but I still put it in my example, though admittedly I was trying it with and without and it just happened that I had it in at the time. Even so, pointless!

    @dklynn I tried your example and had to make a slight change to make the redirected URL the full URL:

    Code:
    RewriteRule ^(category/)?item(/|\.html/)?$ http://www.domain.com/category/item.html [R=301,L]
    That did the job perfectly and also got around another problem I was having further down where another part of the rewrites were appending query string data to the URL so it was becoming:

    http://www.domain.com/category/item.html?url=item

    Thank you very much

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh no, it hasn't worked after all... I hadn't realised but it only redirects from these:


    • item
    • item/
    • item.html/
    • category/item
    • category/item/
    • category/item.html/


    I also need it to redirect from item.html but then it ends up in an infinite loop again because it still sees item.html in the final path:
    Code:
    RewriteRule ^(category/)?item(/|\.html/|\.html)?$ http://www.domain.com/category/item.html [R=301,L]

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This seems to work:
    Code:
    RewriteRule ^category/item(/|\.html/)?$ http://www.domain.com/category/item.html [R=301,L]
    RewriteRule ^(?!category/)item(/|\.html/|\.html)?$ http://www.domain.com/category/item.html [R=301,L]
    Not the pretiest, admittedly, but I can't work out any other way of doing it :/

  7. #7
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dklynn View Post
    Good try! The ? in regex is "zero or one of the preceding character" but the square brackets you referenced could specify the character as being in an allowable set, i.e., it has nothing to do with grouping. Your use of the parenthetical does group and, if followed by a ? will make the entire group optional.
    Er ... huh? The only square brackets that I refered to or featured in my post were the control codes (I don't know if there's a more technical name for them than that) at the end of the lines...

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Stevie,

    What square bracket comment? It must have been in another thread but it was not aimed at you; it referred to that OP's attempting to match Google's encoding of the ?, = and & characters within regex expressions.

    Antnee,

    You specified the series of item, item/, item.html/, category/item, category/item/ and category/item.html/ so that's EXACTLY what you got! Okay, okay, a change in specification brings:

    Code:
    RewriteEngine on
    
    RewriteRule ^(item\.html|(category/)?item(/|\.html/))?$ category/item.html [R=301,L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    698
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @dklynn thanks for the updated rule, that's brilliant. I haven't changed my specification mind you (counted them to make sure I'm not going mad )

    All this assistance is greatly appreciated

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Antnee,

    Wait while I take off my shoes ... aw, you're too right! I hadn't considered that case in the logic. 'Glad it's working for you now.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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