问题
I programmed a website that I want to be multilingual, the separate languages should be accessable via subdomain, for example en.example.com, de.example.com and so on.
Furthermore I already have a rewrite to set a "directory" as GET variable, that changes example.com/name to example.com/index.php?page=name.
No matter what I tried I find no possibilities to properly combine the two codes I need to use with these respective problems. Each in it's own works, I only need a method to combine both, so that en.example.com/name will be rewritten to example.com/index.php?page=name&lang=en
What I use for directories to GET variables
RewriteRule ^/?(\w*)/?$ index.php?page=$1 [L]
What I found for subdomains to GET variables
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule .* /index.php?lang=%1 [L]
回答1:
You just have to combine your 2 rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php?page=$1&lang=%1 [L]
来源:https://stackoverflow.com/questions/25582265/mod-rewrite-setting-subdomain-and-directory-to-get-variable