Wildcard .htaccess rewrite subdomain to a subdirectory with subdomain as GET variable

瘦欲@ 提交于 2019-12-12 03:18:38

问题


Sample Urls:

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

Rewrite to:

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.

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.

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>

Please consider that I am a .htaccess noob.


回答1:


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]


来源:https://stackoverflow.com/questions/38713867/wildcard-htaccess-rewrite-subdomain-to-a-subdirectory-with-subdomain-as-get-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!