首页 > 解决方案 > .htaccess 子目录中语言检测的规则

问题描述

我有一个网址是https://www.myurl.com/subdir/language/login.php.
“语言”参数不是现有的子文件夹。文件夹结构为:

root/subdir/.htaccess

如您所见,.htaccess 文件位于https://www.myurl.com/subdir/.htaccess

我想做一个 .htaccess 重写规则,以便将调用的 uri:(
https://www.myurl.com/subdir/en/login不带 .php)
更改为:
https://www.myurl.com/subdir/login.php?lng=en

我目前的方法是:

# MultiViews must be disabled for the rewrite to work
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteBase /subdir/

RewriteRule (en|de)(/login)$ login.php?lng=$1 [L]

但是当我调用 URI 时,https://www.myurl.com/subdir/en/login我得到一个 http 错误代码404

标签: phpregex.htaccessurl-rewriting

解决方案


对,就是那样!还有一个问题,如果场景都是一样的,但我想(en/de)在前面/subdir/呢?所以网址是myurl.com/en/subdir/login

在这种情况下,必须将规则放在站点根目录 .htaccess 中,而不是subdir/.htaccess使用此规则:

Options -MultiViews
RewriteEngine On

RewriteRule ^(en|de)/(subdir/login)/?$ $2.php?lng=$1 [L,QSA,NC]

推荐阅读