SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write .htaccess OK?

    I'm newbie.
    I'm building website by PHP/MySQL. While i setup file .htaccess, it have this code. However i don't know right or wrong?

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/ $1 [R=301,L]

    Who can help me know it right or wrong?
    Last edited by cpradio; Jun 29, 2013 at 17:03. Reason: Removed unnecessary links

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    t-hot,

    What is your problem with this code?

    It appears that you're trying to do two operations with a single RewriteRule: Redirect 404's to ??? and force non-www'd URLs.

    Personally, I'd use (assuming 404.php as your 404 handler):

    Code:
    RewriteEngine on # Ensure not in comment mode
    
    # enforce non-www
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC] # excellent
    RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
    # you don't need to capture the Apache value you already have ({REQUEST_URI})
    
    # handle 404's
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # RewriteCond %{REQUEST_FILENAME} !-l - symbolic links are rarely used so this is superfluous
    RewriteRule .? /404.php?uri=$1 [R=301,L]
    # the 404.php will be looked for in the server root THEN in your DocumentRoot
    # I retained the {REQUEST_URI} for 404.php to deal with as you see fit
    What did you do wrong? You ANDed all your conditions with the "catch all" in the rule but the redirection loops on the {REQUEST_URI} value so you also need to provide an exit to the loop (other than your file and directory tests).

    For more information, please have a read of the mod_rewrite tutorial Article linked in my signature. It's received rave reviews from all members except one (see next post?).

    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 Member RH-Calvin's Avatar
    Join Date
    Jun 2013
    Location
    Las Vegas
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's full Tips and Tricks for your. Htaccess File: allcodingtips.com/tips-and-tricks-for-your-htaccess-file/ .Hope this will solve your issue.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by RH-Calvin View Post
    Here's full Tips and Tricks for your. Htaccess File: allcodingtips.com/tips-and-tricks-for-your-htaccess-file/ .Hope this will solve your issue.
    Those examples are pretty bad. Mostly because of spaces in the code that shouldn't be there / be somewhere else, plus some stuff that's blatantly wrong, like All Options + Indexes, which must be Options + Indexes (i.e., without the All at the start).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •