htaccess子域(第2部分)

时间:2011-04-13 13:17:08

标签: .htaccess

在我上一篇文章之后,我取得了一些进展,但仍然需要帮助。 对于那些首先阅读这篇文章的人来说,这是一个概要。

好的,项目希望每个注册成员都有一个唯一的URL(子域),例如用户foobar将有foobar.example.com。

经过一番阅读后,我已要求我的托管公司启用名称服务器和Web服务器以接受通配符请求。

这个项目的部分我认为我已经设法解决/创建。 现在我需要找到一种在子域中创建子页面的方法。

目的是demo.example.com和demo.example.com/以及demo.example.com/index.php,由www.example.com/sub_index.php?user=demo

提供服务

demo.example.com/gallery.php由www.example.com/sub_gallery.php?user=demo

提供服务

demo.example.com/gallery.php?page=2由www.example.com/sub_gallery.php?user=demo&page=2

提供服务

子域中的所有页面都具有相同的模式:[sub _]。

基于以上所述,我构建了一些htaccess规则。但是我得不到好结果。

实际上: 对demo.example.com的请求工作完美 对demo.example.com/或demo.example.com/index.php的请求显示了example.com/index.php 对demo.example.com/gallery.php的请求会抛出404错误(尽管页面sub_gallery.php位于根文件夹中)。

我当前的htaccess(在根文件夹中)是:

Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /

#rewrite example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]



###########################
##  subdomains
###########################
    #index
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]

    #other pages
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^/other_page.php$ /sub_other_page.php?user=%1 [QSA,L]

任何人都可以看到任何错误并提供一些反馈吗? 谢谢

2 个答案:

答案 0 :(得分:1)

尝试将您的上一条规则更新为以下规则。它对于请求的页面gallery.php - >是动态的。 sub_gallery.php,我删除了前导/,因为从.htaccess文件重写时不存在。

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*).php$ /sub_$1.php?user=%1 [QSA,L,R]

希望这有帮助。

答案 1 :(得分:0)

我只是想在这里发布最终代码,供将来的读者使用

#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###########################
## subdomains
###########################
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L]

此代码将采用名为:sub_ [PAGE] .php?user = demo& id = 5的任何页面 并创建demo.example.com/ [PAGE] .php?id = 5