开发者

url rewrite with htaccess

开发者 https://www.devze.com 2023-04-04 12:08 出处:网络
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.

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.blogh开发者_运维技巧ost.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?


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.


@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号