问题
I have been struggling with this problem on hostgator shared hosting server for the past 3 hours. This is the problem in the simplest.
This is the mapping i want to happen: subdomain.domain.com --> www.domain.com/test.php?user=subdomain
but for some reason whenever i enter "subdomain.domain.com" the redirection doesn't happen and even the URL doesn't change in my browser. And I see test.php file listed on loaded page (because The only files i have on server are .htaccess and test.php).
This is the .htacess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
<IfModule mod_proxy.c>
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).domain.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/test.php?user=$1$2 [P,L]
</IfModule>
<IfModule !mod_proxy.c>
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).domain.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/test.php?user=$1$2 [R=301,L]
</IfModule>
</IfModule>
and this is the test.php code
<?php echo $_GET['user']; ?>
does anyone know what the issue here is? the support was not helpful to me. i think there is something wrong with the server configuration.
回答1:
RewriteCond
backreferences use a percent, not dollar sign! %1 not $1.
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).domain.com(.*)$ [NC]
^ %1 ^ %2
RewriteRule ^(.*)$ http://www.domain.com/test.php?user=$1$2 [R=301,L]
^$1 ^^^^ oops
$1
means pull something from the regex on the left; %1 means pull it from the RewriteCond
. You may be sticking the whole match as the user part, assuming the rewriter doesn't choke on the fact that $2
is a dangling backreference.
来源:https://stackoverflow.com/questions/9632242/hostgator-wildcard-subdomain-mapping-to-url