Wildcard domain and links to pages in site root
I have a wildcard subdomain (ServerAlias *.myd开发者_开发问答omain.com) to catch all subdomains requests and redirecting them to the home page while keeping the "fake" URL in the browser and passing the subdomain (city name) as a URL parameter
.htaccess in the subdomain folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P,L]
</IfModule>
The problem: there is a menu with some links in every page (register.php, login.php, contact.php) and if you select any of them while being in one subdomain, the request (city.mydomain.com/login.php for example) is captured by the condition/rule
I guess I need to add a second set of condition(s)/rule but after some tests can't find the right one. I added this before the one already working:
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://%2.%3/%4 [P,L]
receiving the error:
Bad Gateway! The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /register.php.
Reason: DNS lookup failure for:
If you think this is a server error, please contact the webmaster.
Error 502 city.domaim.com
CentOS 5 Parallels Small Business Panel 10.2.0
Thanks in advance
You may be over complicating this by using mod_rewrite. I'd suggest simply to parse/validate the subdomain (city) and set a constant with straight PHP. For example in a config/init file. In the end, you'll have far greater control over the subdomain, especially in the case of validation.
KISS :)
The error was a naive one, I overlooked that back-references in the substitution string to RewriteCond pattern (%N), refer to the last one matched. Then, in my original rule, %2, %3 and %4 didn't make any sense
Here is the .htaccess working:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/%1.php [P]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P]
</IfModule>
Besides, the [P] flag implies [L]
The lesson: RTFM! :-)
精彩评论