问题
I'm trying to write a mod_rewrite to work like this
wildcard.subdomain.domain.com
=>subdomain.domain.com/data.php?q=wildcard
wildcard.subdomain.domain.com/?repeat=1
=>subdomain.domain.com/data.php?q=wildcard&repeat=1
subdomain.domain.com
=>data.php
www.subdomain.domain.com
=>data.php
What I've done so far in my .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
RewriteCond %1 !^(www)$ [NC]
RewriteRule ^.*$ data.php?q=%1
</IfModule>`
回答1:
Try these:
# This should cover the first 2 cases
RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
RewriteCond %1 !^(www)$ [NC]
RewriteRule ^$ /data.php?q=%1&%{QUERY_STRING} [L,QSA]
# This should cover the last 2
RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
RewriteCond %1 ^(www)$ [NC]
RewriteRule ^$ /data.php?%{QUERY_STRING} [L,QSA]
Note that this does nothing in the cases where the request is: wildcard.subdomain.domain.com/some/path/to/file.html
These only match request URI's that are /
.
回答2:
For repeat parameter use
RewriteRule ^.*$ data.php?q=%1&%{QUERY_STRING}
回答3:
if you are using a cpanel , you can create a * sub domain (*.domain.com) and connect it to a directory , then create a index.php in that directory :
<?php
echo "<pre>";
print_r($_SERVER);
echo "</pre>";
?>
test the result , you can export sub domain names from server name or server host
来源:https://stackoverflow.com/questions/11568569/htaccess-rewrite-wildcard-subsubdomain-and-params