我想要使用htaccess重写子域到一个获取参数,但保持在域完好后的所有内容+参数添加到结束或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)的映射。
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代码重写:子域作为GET var和path作为GET var
您可以在您的根.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]