⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

.htaccess file



noskov.biz
  • profile picture
  • Member

Posted 09 July 2012 - 07:19 AM

Hi everybody!

To tell the truth I don't know much about the .htaccess file. At the beginning I had in my .htaccess a few lines that we can find in the CodeIgniter user guide. Then I started to use .htaccess file that is suggested by HTML5 Boilerplate.

So I wonder what options can we use to make it better in our projects with CodeIgniter and Grocery CRUD? And it is really interesting for me to hear your opinion about .htaccess file.

Thanks!

fdias
  • profile picture
  • Member

Posted 09 July 2012 - 23:17 PM

Dear Noskov,

I've used .htaccess files mostly for URL rewrite and basic folder protection.
In CI I've used a great .htaccess file that I shared on stackoverflow and will share with you as well.

Find the file on the link below:

http://stackoverflow.com/questions/11057519/codeigniter-and-mod-rewrite-with-multiple-applications/11057652#11057652

Let me know if you have any questions.

Cheers!

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 10 July 2012 - 06:47 AM

I always use a simple one like for 4 years and it works everywhere:


# Turn on URL rewriting
RewriteEngine On

RewriteBase /

# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs|templates/(.+)/(.+)/template\.php)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|opensearch\.xml|favicon\.ico|assets|forums)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8


Also an other useful thing is to redirect the base to www with this simple lines of code:

RewriteCond %{HTTP_HOST} ^grocerycrud.com
RewriteCond %{HTTPS} !=on
RewriteRule (.*) http://www.grocerycrud.com/$1 [R=301,L]


You can also download the file [attachment=218:htaccess.txt] (don't forget to rename it to .htaccess)

noskov.biz
  • profile picture
  • Member

Posted 10 July 2012 - 16:33 PM

Hi again and thank you for your replies, my friends!

1) ./application/ & ./system/ protection. I have often seen that developers try to protect these folders as much as possible. But in the Boilerplate .htaccess I found next lines that protect every folder in the project from browsing if it has't index.html file. What do you think about this?


# "-Indexes" will have Apache block users from browsing folders without a default document
# Usually you should leave this activated, because you shouldn't allow everybody to surf through
# every folder on your server (which includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>


2) Flag "passthrough|PT". Hm, I've never seen this before. As I learn today ;), this flag is used when we want to "mix" some directives from different Apache modules. But there is only RewriteRule in your code, web-johnny. Why do you use [PT]? Sorry, if I ask stupid questions.

3) About base redirect. In the Boilerplate .htaccess file there are two variants. The first one redirects from http://[b]www[/b].site.com to http://site.com and the other one does vice versa: from http://site.com to http://[b]www[/b].site.com. I use the first option without "www", it seems to me that users hardly ever type "www" in the browser, so I follow them. What do you think about "www" in the urls?

xxaxxo
  • profile picture
  • Member

Posted 11 July 2012 - 06:40 AM

+1 for the plain url without www - some people dont understand it and think that you are always supposed to use it and other just know ;)
I'm also using the boilerplate .htaccess it great and I think it has the most needed things

fdias
  • profile picture
  • Member

Posted 11 July 2012 - 14:09 PM

web-johnny,

Your .htaccess is very clean and straight forward. I will give it a try.

Cheers.

luki
  • profile picture
  • Member

Posted 30 August 2016 - 13:14 PM

I always use a simple one like for 4 years and it works everywhere:
 

# Turn on URL rewriting
RewriteEngine On

RewriteBase /

# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs|templates/(.+)/(.+)/template\.php)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]

# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|opensearch\.xml|favicon\.ico|assets|forums)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
Also an other useful thing is to redirect the base to www with this simple lines of code:
RewriteCond %{HTTP_HOST} ^grocerycrud.com
RewriteCond %{HTTPS} !=on
RewriteRule (.*) http://www.grocerycrud.com/$1 [R=301,L]
You can also download the file attachicon.gifhtaccess.txt (don't forget to rename it to .htaccess)

 

This doesn't work for me on CI 3.x

my CI config:

$config['base_url'] = 'http://mypc.local';

$config['index_page'] = '';

$config['uri_protocol'] = 'REQUEST_URI';

 

it works for http://mypc.local/home

it works for http://mypc.local/index.php/home

it works for other websites which don't utilize GC

it works for http://mypc.local/index.php/examples (GC)

 

BUT not for http://mypc.local/examples (GC)

it shows:

Service unavailable!

 

anyone can help?