通配符.htaccess将子域重写为以子域为GET变量的子目录 [英] Wildcard .htaccess rewrite subdomain to a subdirectory with subdomain as GET variable

查看:61
本文介绍了通配符.htaccess将子域重写为以子域为GET变量的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样品尿:

http://username1.example.com/inventory/
http://username2.example.com/profile/about
http://example.com/products/category/

重写为:

http://example.com/subdomains/inventory/?user=username1
http://example.com/subdomains/profile/about?user=username2
http://example.com/products/category/

我想我已经在互联网上搜索了很多.

I think I have searched most of the internet for this.

如何通过在域后立即添加"subdomains"文件夹来重写路径,并将子域作为GET变量传递,以通过PHP访问它?用户在此地址栏中看到的网址不应有任何变化. 没有子域时,不应进行任何更改或重写.

How do I rewrite the path by adding "subdomains" folder immediately after the domain and pass the subdomain as GET variable to access it via PHP? There shouldn't be any change in the url which the user can see in this address bar. There shouldn't be any change or rewrite when there is no sub domain.

现在,我可以像下面这样的GET变量来获取路径和子域,但这不是我想要的.

Right now, I can get both path and sub domain as GET variables like below but that is not what I want.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ /index.php?subdomain=%1&request=$1 [L]
</IfModule>

请认为我是.htaccess新手.

Please consider that I am a .htaccess noob.

推荐答案

尝试一下(如果您已经有其他重写规则,则将其放在最前面):

Try this (if you already have some other rewrite rules, put these on top):

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteRule ^ http://example.com/subdomains%{REQUEST_URI}?user=%1 [QSA,L,P]

这篇关于通配符.htaccess将子域重写为以子域为GET变量的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆