Mod_Rewrite setting subdomain and directory to GET variable

不想你离开。 提交于 2020-01-04 04:01:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!