PHP 的“lang?=”的 mod_rewrite超过一页?

发布于 2024-09-13 03:32:06 字数 532 浏览 1 评论 0原文

我目前在 .htaccess 文件中使用以下代码

RewriteEngine On
RewriteBase /

# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

使用该代码,每次我在 URL 末尾键入 /en 时,它都会将我重定向到 /?lang=en(从 PHP 数组加载英文内容):

例如:

example/en 将我重定向到 example/?lang=en,同时保留 example/en在网址中。

但我还有一个 thanks.php 页面,上面的代码显然只适用于 index.php 页面。

如何使重写规则适用于 index.phpthanks.php 页面?

I currently use the follwoing code in my .htaccess file

RewriteEngine On
RewriteBase /

# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):

For instance:

example/en redirects me to example/?lang=en while keeping example/en in the URL.

But I also have a thanks.php page and the code above clearly just work for the index.php page.

How can I make the rewrite rule to work for both index.php and thanks.php page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-09-20 03:32:06

最直接的方法就是这样做:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]

如果您想让它更通用,您可以选择将文件列入白名单,如下所示:

RewriteCond $1    ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]

...其中 (thanks) 将是您想要具有此功能的文件的管道分隔列表,或者您可以接受每个请求作为对现有 PHP 页面的传递:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]

The most straight-forward way is just to do this:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]

If you want to make it more general, you have the option of white-listing files, like this:

RewriteCond $1    ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]

...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文