SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess coding query - deleting PHP rewrite code

    I have this code in my .htaccess file. I have forgotten what each line of code is doing!

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
    RewriteRule .? http://www.sitename.co.uk%{REQUEST_URI} [R=301,L]
    Options -MultiViews
    RewriteCond %{REQUEST_URI} ^/productcode
    RewriteCond %{QUERY_STRING} product=([a-zA-Z0-9-]+)&code=([a-zA-Z0-9-]+)
    RewriteRule .? /%1%2? [L,R=301]
    We decided not to use PHP on the website. I think this means I can delete this code from the .htaccess file, right?:

    Code:
    Options -MultiViews
    RewriteCond %{REQUEST_URI} ^/productcode
    RewriteCond %{QUERY_STRING} product=([a-zA-Z0-9-]+)&code=([a-zA-Z0-9-]+)
    RewriteRule .? /%1%2? [L,R=301]
    Can you also tell me what this code is doing, please?:

    Code:
    RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
    RewriteRule .? http://www.sitename.co.uk%{REQUEST_URI} [R=301,L]
    The reason for getting rid of the PHP rewrite code is we do not need it and it is causing some errors in Google Analytics (suggesting we have some PHP files with errors when we do not).

    I look forward to your comments,

    Matt.

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

    Oh, my! I just (politely) chastised another member for not knowing what his code is doing ... but you do get credit for asking.

    Quote Originally Posted by MatthewBOnline View Post
    I have this code in my .htaccess file. I have forgotten what each line of code is doing!

    Code:
    RewriteEngine on
    
    Ensures that mod_rewrite is NOT in it's "Comment Mode"
    RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
    Tests whether the {HTTP_HOST} is NOT www.sitename.co.uk without regard to the case (No Case) - but you forgot to escape the . before uk.
    RewriteRule .? http://www.sitename.co.uk%{REQUEST_URI} [R=301,L]
    If the RewriteCond is TRUE, redirect the request to the www'd domain with a permanent redirect.
    Options -MultiViews
    A server directive (I put those at the top to keep the mod_rewrite code contiguous), this tells the server NOT to capture path names and look for files of that name in the path, i.e., the {REQUEST_URI} should look in the specified directory for the specified file.
    RewriteCond %{REQUEST_URI} ^/productcode
    If the request starts with /productcode - which should cause problems with Apache 2.x servers as they do not like a leading / after the start anchor (at least this was the crippling change when converting from an Apache 1.x to the early Apache 2.x servers).
    RewriteCond %{QUERY_STRING} product=([a-zA-Z0-9-]+)&code=([a-zA-Z0-9-]+)
    AND, if the query string contains product={at least one letter, digit or -} followed by & followed by code={at least one letter, digit or -}; WARNING: the order of your key/value pairs is critical! Also, mod_rewrite prefers dashes as dashes to be the first character in the character range definition albeit it should not be confused as a metacharacter as the last character, either.
    RewriteRule .? /%1%2? [L,R=301]
    Permanently redirect to the server's root (or DocumentRoot if not in the server - SECURITY issue) {product value} / {code value}. NOTE: This will require another redirection as the server is not likely to be able to serve anything using product and code values.
    We decided not to use PHP on the website. I think this means I can delete this code from the .htaccess file, right?:

    Sorry, without knowing more about what you're doing, there's no way to tell.

    Code:
    Options -MultiViews
    RewriteCond %{REQUEST_URI} ^/productcode
    RewriteCond %{QUERY_STRING} product=([a-zA-Z0-9-]+)&code=([a-zA-Z0-9-]+)
    RewriteRule .? /%1%2? [L,R=301]
    As above.

    Can you also tell me what this code is doing, please?:

    Code:
    RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
    RewriteRule .? http://www.sitename.co.uk%{REQUEST_URI} [R=301,L]
    As above, it's forcing www on the domain name.

    The reason for getting rid of the PHP rewrite code is we do not need it and it is causing some errors in Google Analytics (suggesting we have some PHP files with errors when we do not).

    That's a reason to change all your code? Oh, well.

    I look forward to your comments,

    Matt.
    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
  •