0

In my current project, I was asked to provide blog feature for every registered user, that means when you register, you can automatically write blogs on site. This was easily done, however, I was instructed to use URLs in format like http://username.site.com refering with username to specific user blog.

I am trying to achieve this with .htaccess file, but it seems my conditions are met but I am getting 500 Internal Server Error, wich is caused by infinite loop of rewrites. I would like to avoid that but I can't find suitable solution. Here is my .htaccess so far:

<IfModule mod_rewrite.c>

   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteBase /

   RewriteCond $1 !^(index\.php|images|tinymce|files|css|js|robots\.txt)
   RewriteRule ^(.*)$ /index.php/$1 [L]

   RewriteCond %{HTTP_HOST} !^www. [NC]
   RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-_]+).dev.example.com [NC]
   RewriteRule ^(.*)$ /index.php/blog/%2/$1 [L]

</IfModule>

Additional info: The site is server hosted, but hidden and on dev.example.com so blog URL's should look like username.dev.example.com, when site will be launched, this will of course be username.example.com. It's built with Codeigniter.

Any suggestions about improving that .htaccess would be great, thank you.

lucasf991
  • 31
  • 6

1 Answers1

0

The best way to do this is not using .htaccess.

This is what I would do:

  1. Create a wildcard DNS record *.example.com
  2. Create a blog modal, pick up $_SERVER['HTTP_HOST'], explode it and remove your domain. Do the lookup in your DB table.

EDIT:

  1. Create a wildcard DNS record, lets say create an CNAME *.example.com > www.example.com This way, when I goto kyle.example.com its really loading www.example.com.

  2. Create a blog model:

row_count() > 0) { $blogInfo = [ 'blog_id'=>123, // value from db 'blog_name'=>'Kyles Test Blog', ... ]; $this->session->set_userdata($blogInfo); return $blogInfo; } else { return false; } } } ?>

// controller

session->userdata('blog_id')) { // blog id not saved session $this->load->model('blog_model'); // load the blog model $url = str_replace('.example.com', '', strtolower($_SERVER['HTTP_HOST'])); // get the current hostname, eg kyle.example.com if($url !== 'www') { if($blog_info = $this->blog_model->checkForValidBlog()) { // load your blog redirect('/blog/' . $blog_info['blog_id'] . '/'); } else { show_error('Error - Blog not found', 404); } } } else { // load your blog redirect('/blog/' . $blog_info['blog_id'] . '/'); } } } ?>
Kyle Hudson
  • 898
  • 1
  • 14
  • 26
  • I have come across this but didn't understand it. If you come to PC or laptop, could you elaborate please? Thank you – lucasf991 Mar 01 '13 at 17:18