【问题标题】:Rewrite URL for multiple languages using .htaccess使用 .htaccess 重写多种语言的 URL
【发布时间】:2013-10-31 19:25:30
【问题描述】:

我正在开发一个多语言网站,但在重写 URL 时遇到了一些麻烦。

我必须有这样的东西:

1) mysite.com/en
2) mysite.com/en/product-one
3) mysite.com/en/product-one/id25/title-page-fake.html
4) mysite.com/en/login.php

网址重定向到:

1) mysite.com/index.php
2) mysite.com/index.php
3) mysite.com/item.php?id=25
4) mysite.com/login.php

我的 .htaccess 文件目前如下所示:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.+/{0,0})$ $1/index.php [NC]
RewriteRule ^([a-z0-9\-]+/)(.*)$ $2 [NC]
RewriteRule ^id(\d+)\/.*$ item.php?id=$1&%{QUERY_STRING} [L]

有效,但抛出异常:

由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯。

【问题讨论】:

  • 这些规则对你有用吗?
  • 那有什么问题:)
  • .htaccess 规则导致无限循环...
  • 这意味着它不能正常工作?
  • 这个.htaccess/的位置是什么

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

好的,试试这个规则:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# requirements 1 & 2
RewriteRule ^en(/product-one)?/?$ /index.php [L,NC]

# requirement # 3
RewriteRule ^en/product-one/id([0-9]+) /item.php?id=$1 [L,NC,QSA]

# requirement # 4
RewriteRule ^en/([^.]+\.php)$ /$1 [L,NC]

让我知道它是否有效,然后我也可以建议其他规则。

【讨论】:

  • 是的,对于 3 和 4,我将提供我的答案。
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2011-04-11
  • 1970-01-01
相关资源
最近更新 更多