【发布时间】:2019-03-06 07:48:05
【问题描述】:
我有以下情况:
https://example.com/index.php?id=4 从数据库中获取页面及其别名并正确重写。我为此使用了以下 htaccess 规则:
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
以上操作正常。
我现在在其中添加了一个名为 lang 的获取参数的语言。
例如,https://example.com/index.php?id=4 的结果为 https://example.com/about,页面显示正确。要获得它的法语版本,我输入https://example.com/about?lang=fr。这也可以正常工作并获取正确的页面详细信息。
在上面的示例中,我要做的是使页面可以通过https://example.com/fr/about 访问。
为此,我在 .htaccess 中添加了如下内容:
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# If just the language is specified (ie example.com/en)
RewriteCond %{REQUEST_URI} ^/..$ [NC]
RewriteRule ^(.*)$ $1/
# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301]
# If no page specified, default to home.
RewriteCond %{REQUEST_URI} !^/../.+ [NC]
RewriteRule ^/(..) /$1/ [R=301]
# If no ln query, tack it on
RewriteCond %{QUERY_STRING} !lang= [NC]
RewriteRule ^/(..)/(.*) /$1/$2?lang=$1 [R=301]
但我得到一个无限重定向循环,并且 URL 显示如下:
https://example.com/enindex.php?q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=en
我好像快到了,有人可以帮忙吗?
【问题讨论】:
标签: php html apache .htaccess web