Ivan
Ivan

Reputation: 453

url rewrite with htaccess

I will inform you briefly what I am doing and where I will need your help.

I am building blog hosting site. I finished CMS and now i am doing user pages.

My idea was to create .htaccess rules so every user and blog owner will have its own blog URL, like: XXX.bloghost.com, where XXX is blog name or blog owner name.

Just to mention that I will use 2 more parameters in URL so here are examples of URLs:

XXX.bloghost.com -> www.bloghost.com/index.php?user=XXX

XXX.bloghost.com/NEWS -> www.bloghost.com/index.php?user=XXX&category=NEWS

XXX.bloghost.com/NEWS/SAMPLE -> www.bloghost.com/index.php?user=XXX&category=NEWS&post=SAMPLE

Here is my current .htaccess file (UPDATED):

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.bloghost.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).bloghost.com [NC]
RewriteRule (.*) %2/$1 [L]

RewriteRule (.+)/(.+)/(.+) blog.php?blog_id=$1&category=$2&post=$3 [L,QSA]
RewriteRule (.+)/(.+) blog.php?blog_id=$1&category=$2 [L,QSA]
RewriteRule (.+) blog.php?blog_id=$1 [L,QSA]


ErrorDocument 404 /error.php

I would really need some help with this. I tried and followed several tutorials on this but with no luck.

Also, can this .htaccess block or redirect all mistyped or not existing URLs to some page?

Upvotes: 0

Views: 263

Answers (2)

atma
atma

Reputation: 875

At first, you must configure you subdomains for the wildcard

ServerAlias www.domain.com domain.com *.domain.com

And be sure that DNS configured for wildcards too. Than use something like this:

RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [QSA]

So after this page http://some.domain.com/news will go to http://www.domain.com/some/news And than process the url as usual (just for example):

RewriteRule (.+)/(.+)/(.+) index.php?user=$1&category=$2&post=$3 [L,QSA]
RewriteRule (.+)/(.+) index.php?user=$1&category=$2 [L,QSA]
// and so on

But I think that second part should be handled with php routing system.

Upvotes: 3

Surreal Dreams
Surreal Dreams

Reputation: 26380

@atma already handled the real redirection. Just a quick add-on for error handling in .htaccess:

ErrorDocument 404 /error.php

Easy! Any 404 error will be directed to bloghost.com/error.php. Substitute your path and page/script to display for errors.

Upvotes: 0

Related Questions