简体   繁体   中英

.htaccess rewrites for subdomain

I want to make all URLs of the site belong to the root domain.com even the assets, and only categories in sub domains

example

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

each of these url take from ===

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

I have this code but its not working

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]

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]

If your Apache configuration does not do this for you, add this

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

If I understand you at all. First, you will need to update your Apache config or Virtual Host ServerAlias to include any subdomains *.example.com you want Apache to respond to. You will probably need to make a wildcard subdomain DNS entry so it knows where to send the request when someone types in games.example.com .

Then you will need to make a rule to exclude www and non-www by using these rules below.

#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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM