.htaccess 如何将domain/en解释为domain?lang=en

t1rydlwq  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(122)

子域示例-lorem.example.com
里面唯一一页-index.php
和一个名为lang$_GET参数
lorem.example.com/en-应解释为-lorem.example.com?lang=en
lorem.example.com/de-应解释为-lorem.example.com?lang=de
这是我尝试,没有成功

RewriteEngine ON
RewriteRule ^(en)/?$ $1.php [QSA,NC,L]  
RewriteRule ^(de)/?$ $1.php [QSA,NC,L]
4jb9z9bj

4jb9z9bj1#

您可以使用此规则:

DirectoryIndex index.php
RewriteEngine On

RewriteRule ^(en|de)/?$ index.php?lang=$1 [QSA,NC,L]

在这里:

  • DirectoryIndex对于在请求仅为http://lorem.example.com时提供index.php非常有用(如果Apache配置中未定义)
  • (en|de)匹配并捕获$1ende,用作lang参数中值
omjgkv6w

omjgkv6w2#

请使用显示示例尝试以下.htaccess规则文件请确保在测试URL之前清除浏览器缓存
请确保将.htaccess和index.php文件保存在同一目录中。

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$  index.php?lang=$1 [QSA,L]

相关问题