.htaccess重写子域

时间:2015-05-05 13:38:00

标签: php apache .htaccess

我想让网站的所有网址都属于root domain.com,即使是资产,也只属于子域中的类别

示例

domain.com/
domain.com/join.php
domain.com/admin/
games.domain.com/
games.domain.com/arcade/
games.domain.com/arcade/10.html

这些网址中的每一个都来自===

domain.com/ === index.php
domain.com/join.php === join.php
domain.com/admin/ === /admin/
games.domain.com/ === main_categories.php?slug=games
games.domain.com/arcade/ === sub_categories.php?slug=games&cat=arcade
games.domain.com/arcade/10.html  === view.php?slug=games&cat=arcade&id=10

我有这段代码,但它不能正常工作

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com 
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain.com 
RewriteRule .* - [S=3]
RewriteRule ^(.*)$ main_categories.php?prefix=%1 
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ sub_categories.php?prefix=$1&type=$3 [QSA,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?.html$ view.php?prefix=$1&id=$2&title=$3 [QSA,L]

2 个答案:

答案 0 :(得分:2)

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.+).domain.ru$ 
RewriteRule ^(.+)/(.+)\.html$ /view.php?slug=%1&cat=$1&id=$2 [L]
RewriteCond %{HTTP_HOST} ^(.+).domain.ru$ 
RewriteRule ^(.+)/$ /sub_categories.php?slug=%1&cat=$1 [L]
RewriteCond %{HTTP_HOST} ^(.+).domain.ru$ 
RewriteRule ^$ /main_categories.php?slug=%1 [L]

如果您的Apache配置不适合您,请添加此

RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^$ /index.php

答案 1 :(得分:1)

如果我完全理解你的话。首先,您需要更新Apache配置或虚拟主机ServerAlias以包含您希望Apache响应的任何子域*.example.com。您可能需要创建一个通配符子域DNS条目,以便当有人在games.example.com中输入时,它知道发送请求的位置。

然后,您需要制定规则,以便使用以下规则排除wwwnon-www

#games.domain.com/ === main_categories.php?slug=games
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^ main_categories.php?slug=%1 [L,QSA]

#games.domain.com/arcade/ === sub_categories.php?slug=games&cat=arcade
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^([^/]+)/?$ sub_categories.php?slug=%1&cat=$1 [L,QSA]

#games.domain.com/arcade/10.html  === view.php?slug=games&cat=arcade&id=10
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)\.html$ view.php?slug=%1&cat=$1&id=$2 [L,QSA]