【问题标题】:Mod_Rewrite setting subdomain and directory to GET variableMod_Rewrite 将子域和目录设置为 GET 变量
【发布时间】:2014-08-30 12:17:34
【问题描述】:

我编写了一个我想要多语言的网站,应该可以通过子域访问单独的语言,例如 en.example.com、de.example.com 等。

此外,我已经进行了重写以将“目录”设置为 GET 变量,这会将 example.com/name 更改为 example.com/index.php?page=name。

无论我尝试什么,我都找不到将我需要使用的两个代码正确结合这些问题的可能性。每个都有自己的作品,我只需要一个方法将两者结合起来,这样 en.example.com/name 将被重写为 example.com/index.php?page=name&lang=en

我用于获取变量的目录

RewriteRule ^/?(\w*)/?$ index.php?page=$1 [L]

我发现子域获取变量的内容

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule .* /index.php?lang=%1 [L]

【问题讨论】:

  • @hjpotter92 感谢您的提示,但是由于我之前没有使用过该标志,并且示例仅在使用诸如 example.com/name/?lang=en 之类的 URL 时才建议使用它,我似乎无法找到正确插入它的方法。您是否可以举例说明我上面提到的场景应该是什么样子?

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

你只需要结合你的两条规则

RewriteEngine On

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php?page=$1&lang=%1 [L]

【讨论】:

  • 谢谢!使用 $_SERVER 数组解决了这个问题,但很高兴知道以供将来参考:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多