SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Cheltenham, UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sanity check of .htaccess

    I'm getting some intermittent issues with a website running FastCGI and htaccess (under Plesk). I wanted to check my htaccess isn't doing anything silly before I go kick my host(!) so wondered if someone would mind a quick sanity check on my htaccess?

    Code:
    # ----------------------------------------------------------------------
    # Start rewrite engine
    # ----------------------------------------------------------------------
    
    # Turning on the rewrite engine is necessary for the following rules and
    # features. FollowSymLinks must be enabled for this to work.
    
    # Some cloud hosting services require RewriteBase to be set: goo.gl/HOcPN
    # If using the h5bp in a subdirectory, use `RewriteBase /foo` instead where
    # 'foo' is your directory.
    
    # If your web host doesn't allow the FollowSymlinks option, you may need to
    # comment it out and use `Options +SymLinksIfOwnerMatch`, but be aware of the
    # performance impact: http://goo.gl/Mluzd
    
    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
    # Options +SymLinksIfOwnerMatch
      RewriteEngine On
    # RewriteBase /
    </IfModule>
    
    
    # ---------------------------------------------------------------------
    # SEO Preservation - The SEF Preservation society!
    # ---------------------------------------------------------------------
    
    <IfModule mod_rewrite.c>
      RewriteEngine On
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Bouquets(.*)$
      RewriteRule (.*) /section/5/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Bereavements(.*)$
      RewriteRule (.*) /section/29/1/bereavement? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Exclusive(.*)$
      RewriteRule (.*) /section/24/1/exclusive? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Modern(.*)$
      RewriteRule (.*) /section/11/1/modern? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Arranged(.*)$
      RewriteRule (.*) /section/10/1/arranged? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Roses(.*)$
      RewriteRule (.*) /section/6/1/roses? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Spring(.*)$
      RewriteRule (.*) /section/28/1/spring_time? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Hand(.*)$
      RewriteRule (.*) /section/5/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Birthdays(.*)$
      RewriteRule (.*) /section/27/1/birthday? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Plants(.*)$
      RewriteRule (.*) /section/19/1/plants? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Christmas(.*)$
      RewriteRule (.*) /section/21/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Mother(.*)$
      RewriteRule (.*) /section/16/1/mothers_day? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Valentine(.*)$
      RewriteRule (.*) /section/15/1/valentines? [L,R=301]
      
      RedirectMatch 301 ^/weddings\.asp$ /article/17/wedding_flowers
      RedirectMatch 301 ^/weddings-intro\.asp$ /article/17/wedding_flowers
      RedirectMatch 301 ^/weddings-photographers\.asp$ /article/20/wedding_photography
      RedirectMatch 301 ^/weddings-prices\.asp$ /article/18/wedding_prices
      RedirectMatch 301 ^/weddings-reception\.asp$ /article/19/wedding_venues
      RedirectMatch 301 ^/viewproduct\.asp$ /
      
      RedirectMatch 301 ^/find-us\.asp$ /page/contact
      RedirectMatch 301 ^/about-us\.asp$ /article/24/about_bumblebeez_florist
      RedirectMatch 301 ^/location\.asp$ /page/contact
      RedirectMatch 301 ^/news\.asp$ /page/social
      
      RewriteCond %{HTTP_HOST} !^www\.bumblebeezflorist\.com$ [NC]
      RewriteRule ^(.*)$ http://www.bumblebeezflorist.com/$1 [R,L]
    </IfModule>
    
    # ----------------------------------------------------------------------
    # JShop SEF URLs
    # ----------------------------------------------------------------------
    <IfModule mod_rewrite.c>
      Options +SymlinksIfOwnerMatch +MultiViews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*?)/(.*?)$ $1.php?$2 [NC]
    </IfModule>
    
    
    # ----------------------------------------------------------------------
    # Custom 404 page
    # ----------------------------------------------------------------------
    
    # You can add custom pages to handle 500 or 403 pretty easily, if you like.
    # If you are hosting your site in subdirectory, adjust this accordingly
    #    e.g. ErrorDocument 404 /subdir/404.html
    ErrorDocument 404 /templates/bumblebeez/404.html
    ErrorDocument 500 /templates/bumblebeez/500.html
    
    
    # ----------------------------------------------------------------------
    # UTF-8 encoding
    # ----------------------------------------------------------------------
    
    # Use UTF-8 encoding for anything served text/plain or text/html
    AddDefaultCharset iso-8859-1
    
    # Force UTF-8 for a number of file formats
    AddCharset iso-8859-1 .atom .css .js .json .rss .vtt .xml

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

    A sanity check, eh? Okay, comments embedded in your code.

    Code:
    # First, because mod_rewrite is handled AFTER all core directives,
    # I'd move the core directives, like DirectoryIndex and ErrorDocument to the start
    
    # ----------------------------------------------------------------------
    # Start rewrite engine
    # ----------------------------------------------------------------------
    
    # Remove comments (once you understand them)
    # Turning on the rewrite engine is necessary for the following rules and
    # features. FollowSymLinks must be enabled for this to work.
    
    # Some cloud hosting services require RewriteBase to be set: goo.gl/HOcPN
    # If using the h5bp in a subdirectory, use `RewriteBase /foo` instead where
    # 'foo' is your directory.
    
    # If your web host doesn't allow the FollowSymlinks option, you may need to
    # comment it out and use `Options +SymLinksIfOwnerMatch`, but be aware of the
    # performance impact: http://goo.gl/Mluzd
    
    # INSANE TO HAVE THIS CHECKED MULTIPLE TIMES FOR EVERY FILE REQUEST<IfModule mod_rewrite.c>
    # SHOULD be in httpd.conf, not .htaccess  Options +FollowSymlinks
    # Options +SymLinksIfOwnerMatch
      RewriteEngine On
    # WORTHLESS RewriteBase /
    # </IfModule>
    
    
    # ---------------------------------------------------------------------
    # SEO Preservation - The SEF Preservation society!
    # ---------------------------------------------------------------------
    
    # AGAIN??? <IfModule mod_rewrite.c>
    
    # [standard rant #4]
    The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T BE AN IDIOT! If you don't know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server). No, I'm NOT calling you an idiot, simply advising against server abuse!
    [/standard rant #4]
    # AGAIN? RewriteEngine On # For the series below, if you're not going to use the (.*) in the RewriteCond statements, # Simply use .? and drop the start and end anchors. On second thought, simply drop # the start and end anchors and your regex will match the text in the string as you're trying to do. # For the (.*) in the RewriteRule, simply replace them with .? as all you want is a match and redirect. RewriteCond %{QUERY_STRING} ^(.*)category=Bouquets(.*)$ RewriteRule (.*) /section/5/1/bouquets? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Bereavements(.*)$ RewriteRule (.*) /section/29/1/bereavement? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Exclusive(.*)$ RewriteRule (.*) /section/24/1/exclusive? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Modern(.*)$ RewriteRule (.*) /section/11/1/modern? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Arranged(.*)$ RewriteRule (.*) /section/10/1/arranged? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Roses(.*)$ RewriteRule (.*) /section/6/1/roses? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Spring(.*)$ RewriteRule (.*) /section/28/1/spring_time? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Hand(.*)$ RewriteRule (.*) /section/5/1/bouquets? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Birthdays(.*)$ RewriteRule (.*) /section/27/1/birthday? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Plants(.*)$ RewriteRule (.*) /section/19/1/plants? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Christmas(.*)$ RewriteRule (.*) /section/21/1/bouquets? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Mother(.*)$ RewriteRule (.*) /section/16/1/mothers_day? [L,R=301] RewriteCond %{QUERY_STRING} ^(.*)category=Valentine(.*)$ RewriteRule (.*) /section/15/1/valentines? [L,R=301] # RedirectMatch is part of mod_alias, i.e., a core directive so # move to top as these will ALWAYS be run before the mod_rewrite RedirectMatch 301 ^/weddings\.asp$ /article/17/wedding_flowers RedirectMatch 301 ^/weddings-intro\.asp$ /article/17/wedding_flowers RedirectMatch 301 ^/weddings-photographers\.asp$ /article/20/wedding_photography RedirectMatch 301 ^/weddings-prices\.asp$ /article/18/wedding_prices RedirectMatch 301 ^/weddings-reception\.asp$ /article/19/wedding_venues RedirectMatch 301 ^/viewproduct\.asp$ / RedirectMatch 301 ^/find-us\.asp$ /page/contact RedirectMatch 301 ^/about-us\.asp$ /article/24/about_bumblebeez_florist RedirectMatch 301 ^/location\.asp$ /page/contact RedirectMatch 301 ^/news\.asp$ /page/social RewriteCond %{HTTP_HOST} !^www\.bumblebeezflorist\.com$ [NC] # RewriteRule ^(.*)$ http://www.bumblebeezflorist.com/$1 [R,L] RewriteRule .? http://www.bumblebeezflorist.com%{REQUEST_URI} [R=301,L] # Again, all you want is a match and the {REQUEST_URI} variable has the request. # I believe that the default for R is 302 (temporary) and you do want a 301 (permanent) redirect #</IfModule> # ---------------------------------------------------------------------- # JShop SEF URLs # ---------------------------------------------------------------------- OMG! AGAIN AND AGAIN! <IfModule mod_rewrite.c> Options +SymlinksIfOwnerMatch [COLOR="#FF0000"][+MultiViews # Always a "problem child"/COLOR] RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*?)/(.*?)$ $1.php?$2 [NC] # You've GOT to be kidding! # </IfModule> # Apache core directives to the end # ---------------------------------------------------------------------- # Custom 404 page # ---------------------------------------------------------------------- # You can add custom pages to handle 500 or 403 pretty easily, if you like. # If you are hosting your site in subdirectory, adjust this accordingly # e.g. ErrorDocument 404 /subdir/404.html ErrorDocument 404 /templates/bumblebeez/404.html ErrorDocument 500 /templates/bumblebeez/500.html # ---------------------------------------------------------------------- # UTF-8 encoding # ---------------------------------------------------------------------- # Use UTF-8 encoding for anything served text/plain or text/html AddDefaultCharset iso-8859-1 # Force UTF-8 for a number of file formats AddCharset iso-8859-1 .atom .css .js .json .rss .vtt .xml
    [/QUOTE]

    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

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Hi, petersen. dklynn has some great advice... but also some questionable advice that isn't so widely accepted in the web community. I thought you might appreciate a second opinion on a few items.

    dklynn recommended that you remove the explanatory comments. I would strongly advise you to *not* do that. Even if you fully understand everything, you won't always be the only person to maintain this code. The next person will very much appreciate that you kept the comments.

    dklynn was obviously very upset that you checked <IfModule mod_rewrite.c> even just once, much less multiple times. In the strictest technical sense, I'm sure he's right that it adds some overhead. However, that overhead is too small to be significant. You'd have to check <IfModule mod_rewrite.c> several hundred times before you start to notice even a single millisecond delay. I'd personally recommend that you prioritize code safety and readability.

    Finally, dklynn recommended that you replace RewriteRule ^(.*)$ http://www.bumblebeezflorist.com/$1 [R,L] with RewriteRule .? http://www.bumblebeezflorist.com%{REQUEST_URI} [R=301,L]. In this case, I'd only advise you to carefully test before proceeding with that change. Due to all the previous rewrite rules, the path that this rewrite rule matches on may be different from the original request URI, so these two rules may not be interchangeable.
    "First make it work. Then make it better."

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Cheltenham, UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for your comments. I've got a few questions but first here is my modified file:

    Code:
    # ----------------------------------------------------------------------
    # Custom 404 and 500 page
    # ----------------------------------------------------------------------
    
    ErrorDocument 404 /templates/bumblebeez/404.html
    ErrorDocument 500 /templates/bumblebeez/500.html
    
    
    # ----------------------------------------------------------------------
    # UTF-8 encoding
    # ----------------------------------------------------------------------
    
    # Use UTF-8 encoding for anything served text/plain or text/html
    AddDefaultCharset iso-8859-1
    
    # Force UTF-8 for a number of file formats
    AddCharset iso-8859-1 .atom .css .js .json .rss .vtt .xml
    
    # ----------------------------------------------------------------------
    # RedirectMatch part of mod_alias and run before any mod_rewrite so put
    # them here
    # ----------------------------------------------------------------------
    RedirectMatch 301 ^/weddings\.asp$ /article/17/wedding_flowers
    RedirectMatch 301 ^/weddings-intro\.asp$ /article/17/wedding_flowers
    RedirectMatch 301 ^/weddings-photographers\.asp$ /article/20/wedding_photography
    RedirectMatch 301 ^/weddings-prices\.asp$ /article/18/wedding_prices
    RedirectMatch 301 ^/weddings-reception\.asp$ /article/19/wedding_venues
    RedirectMatch 301 ^/viewproduct\.asp$ /
    
    RedirectMatch 301 ^/find-us\.asp$ /page/contact
    RedirectMatch 301 ^/about-us\.asp$ /article/24/about_bumblebeez_florist
    RedirectMatch 301 ^/location\.asp$ /page/contact
    RedirectMatch 301 ^/news\.asp$ /page/social
    
    # ----------------------------------------------------------------------
    # Start rewrite engine
    # ----------------------------------------------------------------------
    
    # Turning on the rewrite engine is necessary for the following rules and
    # features. FollowSymLinks must be enabled for this to work.
    
    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
    
    # ---------------------------------------------------------------------
    # SEO Preservation - The SEF Preservation society!
    # ---------------------------------------------------------------------
    
      RewriteCond %{QUERY_STRING} ^(.*)category=Bouquets(.*)$
      RewriteRule (.*) /section/5/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Bereavements(.*)$
      RewriteRule (.*) /section/29/1/bereavement? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Exclusive(.*)$
      RewriteRule (.*) /section/24/1/exclusive? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Modern(.*)$
      RewriteRule (.*) /section/11/1/modern? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Arranged(.*)$
      RewriteRule (.*) /section/10/1/arranged? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Roses(.*)$
      RewriteRule (.*) /section/6/1/roses? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Spring(.*)$
      RewriteRule (.*) /section/28/1/spring_time? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Hand(.*)$
      RewriteRule (.*) /section/5/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Birthdays(.*)$
      RewriteRule (.*) /section/27/1/birthday? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Plants(.*)$
      RewriteRule (.*) /section/19/1/plants? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Christmas(.*)$
      RewriteRule (.*) /section/21/1/bouquets? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Mother(.*)$
      RewriteRule (.*) /section/16/1/mothers_day? [L,R=301]
      
      RewriteCond %{QUERY_STRING} ^(.*)category=Valentine(.*)$
      RewriteRule (.*) /section/15/1/valentines? [L,R=301]
      
      RewriteCond %{HTTP_HOST} !^www\.bumblebeezflorist\.com$ [NC]
      RewriteRule ^(.*)$ http://www.bumblebeezflorist.com/$1 [R,L]
    
    # ----------------------------------------------------------------------
    # JShop SEF URLs
    # ----------------------------------------------------------------------
    
      Options +SymlinksIfOwnerMatch +MultiViews
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*?)/(.*?)$ $1.php?$2 [NC]
    </IfModule>
    Questions:
    1) Core Directives (I'm assuming that ErrorDocument and AddDefaultCharset classify as this) - One comment said 'put at the top' another said 'put at the bottom' - which is correct?
    2) +Multiviews - why is this a problem child? As I understand, it's required for JShop (what the website is built on)
    3) RewriteRule ^(.*?)/(.*?)$ $1.php?$2 [NC] - what's wrong with this? Again, this is provided by JShop - could it be written neater?

    With regards to the (.*) or .? match, I'm undecided.

    Thank you both.

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

    A1. Both. It's a matter of technique. I use the top as core directives will always take precedence over non-core and mod_rewrite seems to be at the bottom of that list.

    A2. MultiViews tells Apache to serve a file named in the path of the request, i.e., something/index/yadda/yadda/yippee will serve the index.whatever file in the something directory. Not many webmasters know that they will have to avoid file names for common tasks in the path so it's a dangerous thing to play with for that reason. On the other hand, if your CMS is built to do just that (then parse the remainder of the {REQUEST_URI}, then you're stuck with it. Just be cautious when you create files.

    A3. First, (.*) is a dangerous bit of regex as it will match EVERYTHING (or NOTHING) and newbies use it without understanding what that means. It's quite a handy bit of code if you know what dangers to look for (looping code, generally). Adding the ? means that it's non-greedy but what's the relevance to your URIs? IMHO, that's lousy regex, period.

    As for the No Case flag, the {REQUEST_URI} variable is case sensitive so it's clearly an improper thing to use in a RewriteRule (which can only try to match the {REQUEST_URI} variable). The only Apache variable I can think of which you NEED the No Case flag for is the {HTTP_HOST} because it is NOT case sensitive (so you need it to match the variable in case some hacker is using CaMeL CaSe to bypass your RewriteCond statement.

    Q? (.*), when used with the start and end anchors is simply capturing the {REQUEST_URI} variable. Apache 1.x required a leading / to match after the start anchor (in the DocumentRoot) while Apache 2.x refuses to match a specified leading / (again, in the DocumentRoot). Since .? will match anything, it will always be TRUE and {REQUEST_URI} will handle the question of the leading / (^/...) for you. It's a very minor efficiency question but, IMHO, technique and minor savings like that add up to professional coding.

    Remaining comments on your code:

    1. I won't claim to be an expert on mod_alias but it is my understanding that RedirectMatch requires the regex engine while Redirect does not. Since you're not using any regex, you don't need to fire-up the engine so they could all be Redirect statements.

    2. Use of ANY <IfModule> wrapper in .htaccess is an abuse of the server. Understand that mod_rewrite will cycle though your .htaccess file multiple times before no matches are found and redirections are made. With <IfModule> in there (THREE TIMES in your original code), you're simply repeating the same test time after time for every request. Remember my reminder about the definition of an idiot? Are you REALLY expecting a different answer with microseconds (or even months) from your server? If so, then you'll have to wear that label.

    3. You KNOW you'll be abused for the inappropriate use of (.*) so here goes:

    Code:
    RewriteCond %{QUERY_STRING} ^(.*)category=Modern(.*)$
      RewriteRule (.*) /section/11/1/modern? [L,R=301]
    The (.*) before category: What does it do for you (other than capture EVERYTHING or NOTHING)? That's a waste of resources. Very simply, unless you have multiple keys ending in category, you don't need the start anchor and you don't need the (.*).

    The (.*) at the end has the same story except that the value Modern could be Modern%20Age (or other nonsense) or another key=value pair. Why capture it as %2 if you're not going to use it? Another waste of resources.

    So, if all you wanted to match was category=Modern, why waste time (resources) on all the extraneous @#$% when

    Code:
    RewriteCond %{QUERY_STRING} category=Modern
    would do the same job much faster?

    The (.*) in the RewriteRule's regex is simply another waste of time (as discussed above). If you're not using it, .? is not just as good, it's better (because it doesn't waste time waiting for Apache to create a useless $1 variable - MULTIPLE TIMES (again)!

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    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

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Cheltenham, UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Pete,
    3. You KNOW you'll be abused for the inappropriate use of (.*) so here goes:

    Code:
    RewriteCond %{QUERY_STRING} ^(.*)category=Modern(.*)$
      RewriteRule (.*) /section/11/1/modern? [L,R=301]
    The (.*) before category: What does it do for you (other than capture EVERYTHING or NOTHING)? That's a waste of resources. Very simply, unless you have multiple keys ending in category, you don't need the start anchor and you don't need the (.*).

    The (.*) at the end has the same story except that the value Modern could be Modern%20Age (or other nonsense) or another key=value pair. Why capture it as %2 if you're not going to use it? Another waste of resources.

    So, if all you wanted to match was category=Modern, why waste time (resources) on all the extraneous @#$% when

    Code:
    RewriteCond %{QUERY_STRING} category=Modern
    would do the same job much faster?

    The (.*) in the RewriteRule's regex is simply another waste of time (as discussed above). If you're not using it, .? is not just as good, it's better (because it doesn't waste time waiting for Apache to create a useless $1 variable - MULTIPLE TIMES (again)!
    This is handling old URLs (picked from Wbemaster Tools) such as

    /category=Modern%20Flowers&cat=4
    /category=Modern&cat=4
    /index.asp?/category=Modern&cat=4
    and sometimes
    /category.asp

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

    What's your point? Is it that you don't care about efficiency of your code? Wasting server resources? I don't get it.

    Well, you asked for a sanity check ... then argued.

    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

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Cheltenham, UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    pete,

    What's your point? Is it that you don't care about efficiency of your code? Wasting server resources? I don't get it.

    Well, you asked for a sanity check ... then argued.
    Ha ha, not argued, just questioned.

    As I understood it (and some of this is lack of regex understanding I accept), that

    Code:
    category=Modern(.*)
    would match category=ModernANYTHINGELSE

    Which is what I wanted as there could be any number of pairs after and Modern could be Modern%20Flowers

    I also couldn't be sure what was before it, hence the (.*) before the term as well? Is that not the case?

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

    Sorry about the arguing comment but there's one of those in the boards again these days.

    If all you care about is the category=Modern then you need no more than that in the RewriteCond's regex. Too simple? It's just logic. As I stated before, if you're worried about nextcategory=Modern%20Art being confused with category=Modern, then you do need to jump through some hoops but, if you're not capturing and dealing with the content (EVERYTHING or NOTHING) either before or after, you clearly don't need anything else.

    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

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Cheltenham, UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for clearing that up and thanks for your help.

    Sometimes you need someone to show you the error of your ways to learn.

    Cheers

    Pete

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

    You're very welcome. I know I come on too pedantic but that's a remnant of my having been a TL and needing to be sure that all members, not just the OP, understand the question as well as the ramifications of the answers (and it's many options). That does not always come across well but it's left to the reader to assess the personal value.

    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


Tags for this Thread

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
  •