问题描述
我想使用htaccess将子域重写为get参数,但保留域完好后的所有内容+将参数添加到末尾或url中.
I want to use htaccess to rewrite subdomains to a get paramater but keep everything after the domain intact + adding the parameter to the end or the url.
所需结果:
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 = whatever)的映射.
With the .htaccess I have right now, I can achieve the subdomain to parameter (index.php?subdomain=whatever) mapping with the following code.
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
在下面添加了第二个重写,但是此重写不起作用.我该如何调整才能获得理想的效果?
Added a second rewrite below but this one does not work. How can i adjust it so i get the desired results?
# 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中获取了代码重写:子域为GET var,路径为GET var
推荐答案
您可以在根.htaccess中使用此代码:
You can use this code in your root .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]
这篇关于.htaccess重写:子域作为GET参数,文件路径afterdomain完好无损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!