带有语言和页面参数的 htaccess 重定向 URL


htaccess redirect url with language and page parameters

我真的迷失在重定向htaccess的所有规则中,我找不到解决我问题的完美解决方案。我正在尝试重定向如下所示的网址:

http://mywebpage.eu/path/to/site/LANGUAGE/PAGE

谁应该重定向到谁

http://mywebpage.eu/path/to/site/index.php?lang=LANGUAGE&page=PAGE

但有时他们可能会像这样没有页面或语言

http://mywebpage.eu/path/to/site/LANGUAGE/
http://mywebpage.eu/path/to/site/PAGE/

谁应该重定向到谁

http://mywebpage.eu/path/to/site/index.php?lang=LANGUAGE&page=
http://mywebpage.eu/path/to/site/index.php?lang=&page=PAGE

(所有这些示例都可以以"/"结尾或不以"/"结尾)

当我最终得到正在工作的东西的一部分时,我所有的 css/images/js/include(php) 都出现了 404 错误,因为添加了那个虚拟子文件夹"/LANGUAGE/"......

RewriteEngine on
RewriteBase /path/to/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/(.*)$ index.php?lang=$1&page=$2 [L,QSA]

非常感谢您的帮助。

像这样:

RewriteEngine on
RewriteBase /path/to/site/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([a-z]{2})(?:/(.*))?/?$ index.php?lang=$1&page=$2 [L,QSA]
RewriteRule ^('w+)/?$ index.php?lang=&page=$1 [L,QSA]

对于css/js/images,您可以在页面HTML的<base href="/path/to/site/" /> <head>部分的正下方添加以下内容,以便从该基本URL而不是从当前页面的URL解析每个相对URL