Apache .htaccess重写:子域作为GET参数,文件路径保持不变
我想使用htaccess将子域重写为get参数,但保留域后的所有内容,并将参数添加到末尾或url 预期结果:Apache .htaccess重写:子域作为GET参数,文件路径保持不变,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,我想使用htaccess将子域重写为get参数,但保留域后的所有内容,并将参数添加到末尾或url 预期结果: http://mpmain.example.com/ -> index.php http://www.example.com/ -> index.php http://hello.example.com/ -> index.php?subdomain=hello http://whatever.example
http://mpmain.example.com/ -> index.php
http://www.example.com/ -> index.php
http://hello.example.com/ -> index.php?subdomain=hello
http://whatever.example.com/ -> index.php?subdomain=whatever
http://whatever.example.com/index.php?page=login -> index.php?page=login&subdomain=whatever
http://whatever.example.com/index.php?page=about&action=help -> index.php?page=about&action=help&subdomain=whatever
使用我现在拥有的.htaccess,我可以通过以下代码实现到参数index.php?subdomain=的子域映射
RewriteEngine On
# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} !^mpmain
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1
在下面添加了第二次重写,但这一次不起作用。如何调整它以获得所需的结果
# Map all requests to the 'path' get variable in index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?subdomain=$1 [L,QSA]
我从中获取了代码。您可以在根目录中使用此代码。htaccess:
DirectoryIndex index.php
RewriteEngine On
# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(?:www|mpmain) [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.*)$ $1?subdomain=%1 [L,QSA]
感谢您的努力,但是这个似乎无法处理&subdomain=which part.Nada。如果我浏览到whatever.example.com,它不会被重写为whatever.example.com/?subdomain=whater,所以看起来它在那里出错了。这是由于RewriteCond%{REQUEST_FILENAME}造成的-f条件。现在试试,谢谢!现在可以工作了:现在我终于可以享受我的共享用户数据库和共享代码结构了,这是唯一的阻碍。现在开始一些bash编程。