SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question .Htaccess template questions

    Hello all,

    I have made a .htaccess file that servers as a template for future websites.
    Still I'm having problems with some points.
    1. I would like to have trailing slashes in my urls so I need to link "domain/link" to "domain/link/" and this also applies to the "domain/link/link/" form
    2. I to disable index.php direct access. So "domain/index.php" or "domain/index.php?request=about" shouldn't be allowed but "domain/about/" should.

    Here's my .htaccess file.

    Code:
    Options -Indexes
    ServerSignature Off
    
    <files .htaccess>
    deny from all
    </files>
    
    <ifModule mod_php5.c>
    php_value zlib.output_compression 16386
    </ifModule>
    
    <IfModule mod_deflate.c>
    <FilesMatch "\.(css|html|js|php|txt|xml)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    </IfModule>
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
    RewriteRule .*  [F,L]
    
    # With the www. prefix
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    
    # Without the www. prefix
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
    Also, if you have any suggestions for the existing code, please respond!

    Thank you in advance,
    xtaste

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Perhaps not a big deal, but just something to consider... even through rewrite rules get the job done, the Apache docs recommend that redirections be handled with the Redirect directive.
    "First make it work. Then make it better."

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

    If you are a host, you would know that:

    1. Your attempt to force trailing slashes on non-directories is an extremely bad idea.
    2. The assumption that omitting a (DirectoryIndex) filename and using a "directory" and expecting it to be assigned as the value of ANY key and redirected to the presumed DirectoryIndex file is (IMHO) nonsensical.
    3. Using .htaccess to constantly repeat <IfModule> tests multiple times for every request is abusive of a server.
    4. Writing loopy code (note that the www's are contradictory) is ... well, loopy.


    At this point, please allow me to recommend that you reconsider your motive for your ill-conceived questions rather than pursue this exercise.

    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
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,528
    Mentioned
    52 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dklynn View Post

    1. Your attempt to force trailing slashes on non-directories is an extremely bad idea.
    The problem is that google recognizes the url with the trailing slash and the url without the trailing slash as two separate urls. From what I've read, it seems to be a good idea to force urls one way or the other, so long as the url isn't a file name with an extension, or doesn't have a trailing url query.

    Writing loopy code (note that the www's are contradictory) is ... well, loopy.
    It's commented out.

    xtaste, are you using a CMS of any sort? (wordpress, joomla, drupal, etc)
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello all,

    Sorry for my late reply, my pc wouldn't boot anymore, it's temporarily fixed.

    @ Jeff Mott: Thanks for your input! That's something new to me, thanks! But, how to use it in my case? Can I use it to redirect http:// to http://www. (and the other way around)? Or can I use it to redirect everything to index.php?request=example?

    @ dklynn: I have read many posts from google and did my research so...
    1. It's not a bad idea, on the contrary, Google prefers it that way unless you're talking about files (so, with file extension e.g. .jpg, .php, .html, etc)
    2. Sorry, I don't really understand what you mean.
    3. That's true, but I can't see any other way to test if these modules exist. Do you have an alternative? If this htaccess file would be placed on a server that doesn't meet the requirements for the modules and the if-statements wouldn't be there, it would be far more server-heavy, because it would cause errors... or am I wrong?
    4. One of the redirects is commented out. It's this way because it's a template, it must contain all necessary code that can do both; with www and without. I comment out or remove the lines that aren't needed for a website when the website is finished.

    @ Force Flow: Thanks for your reply! I am using my own framework, it takes the request parameter as its input to the bootstrap file.

    Sorry again for my late response,

    Kind regards,
    xtaste

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey all,

    I've been working on the .htaccess file today. This is what I have now, please let me know what you think.

    Code:
    Options +FollowSymLinks
    Options -Indexes
    ServerSignature Off
    
    <files .htaccess>
    deny from all
    </files>
    <files config.php>
    deny from all
    </files>
    
    RewriteEngine On
    
    # BASE PATH
    RewriteBase /
    
    # i've added on more possible harmful bot
    RewriteCond %{HTTP_USER_AGENT} ^libwwwperl [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} ^libwww-perl [NC]
    RewriteRule .* - [F,L]
    
    # IP FIX
    #RewriteCond %{HTTP_HOST} ^([.0-9]+)$ [NC]
    #RewriteRule .* http://www.example.com/ [L,R=301]
    
    # WWW. ON
    #RewriteCond %{HTTP_HOST} !^www\. [NC]
    #RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
    
    # WWW. OFF
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
    
    # this is for the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !.*/$ [NC]
    RewriteRule ^(.*)$ $1/ [L,R=301]
    
    # the redirect to the index.php file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
    I've tried to make the www. version as short as the non-www. version, but that doesn't work.

    Code:
    # WWW. ON
    #RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
    This is probably because of the NOT (!) factor.

    The index.php?request=example problem has been fixed by just adding a canonical meta tag.

    Any suggestions or corrections?

    Thanks in advance,
    xtaste

  7. #7
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xtaste View Post
    I've tried to make the www. version as short as the non-www. version, but that doesn't work.

    Code:
    # WWW. ON
    #RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
    This is probably because of the NOT (!) factor.
    Correct. %1 would be the value matched in the capturing parentheses... except that the condition passes only if the pattern doesn't match. Replacing %1 with %{HTTP_HOST} should probably do the trick. Also, consider copying some examples from the Apache docs.
    "First make it work. Then make it better."

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Jeff,

    Thanks for your reply. I'll try the %{HTTP_HOST} method out tomorrow! That would give me some more freedom in making the htaccess.
    Thanks for the link, I'll give it a try, I'll report tomorrow how it all worked out.

    Kind regards,
    xtaste

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Jeff,

    Thanks for all the help and the Apache docs helped me better understand some parts and made it even easier.
    I still stick with the mod rewrite as I want to be as dynamic as possible, so no www.example.com replacements anymore

    I use this code for the www. prefix.

    Code:
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
    It works great, but when I read the Apache docs I noticed they also gave this option but with the following line added.

    Code:
    RewriteCond %{HTTP_HOST} !^$
    This means the string should not be empty, but if it was they wouldn't be able to visit my website, right?
    I don't really understand what's going on here.

    I also have questions about the NE and R 'tags'. R should contain a code like 301 or 302, right? And doesn't the NE 'tag' complicate things when you have special characters in your urls? (not that I'm fond of using these in an url).

    Thanks again,

    Kind regards,
    xtaste

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xtaste View Post
    I use this code for the www. prefix.

    Code:
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
    Very nice.

    Quote Originally Posted by xtaste View Post
    It works great, but when I read the Apache docs I noticed they also gave this option but with the following line added.

    Code:
    RewriteCond %{HTTP_HOST} !^$
    This means the string should not be empty, but if it was they wouldn't be able to visit my website, right?
    I don't really understand what's going on here.
    I can't think of any scenario either where the host could be blank. My best guess is that during internal testing, they were able to simulate a request with a blank host and needed this line to pass all their test cases.

    Quote Originally Posted by xtaste View Post
    I also have questions about the NE and R 'tags'. R should contain a code like 301 or 302, right? And doesn't the NE 'tag' complicate things when you have special characters in your urls? (not that I'm fond of using these in an url).
    The R flag *can* contain a code like 301 or 302. If it's left off, then the default will be 302, a temporary redirect.

    My best guess for NE is that they thought it might be a common scenario where the new, redirected URL would contain a hash mark, like in their NE flag example. But you seem to be right also, that if, for example, the URL contained an escaped hash mark, then the NE flag would complicate things.
    "First make it work. Then make it better."

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Jeff,

    Thanks for all the replies, no more questions here!

    Kind regards,
    xtaste


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
  •