htaccess 的 RewriteRule 隐藏默认语言

发布于 2024-12-15 17:23:19 字数 1183 浏览 0 评论 0原文

在 Zend 配置默认值中,我有以下规则:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

当我尝试添加下面的行时,不会发生:

RewriteRule ^en/(.*) $1 [L]

结果我期望永久重写 http://example.com/en/somethinghttp://example.com/something 现在我的两个链接都是分开工作的。

编辑:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks

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

RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

RewriteRule ^en/(.*) /$1 [L,R]

这会将默认语言 URL 直接重定向到站点根目录! 非常感谢。

LigHTTPD 有:

$HTTP["url"] != "^/en/" {
    url.rewrite-once = (
        "^/.*" => "index.php?/$1",
    )
}
url.redirect = (
    "^/en/.*" => "/$1",
)

In Zend configuration defaults I have these rules:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

When i try to add fallowing line, notfing happens:

RewriteRule ^en/(.*) $1 [L]

On result I expact to rewrite permomently http://example.com/en/something to http://example.com/something
At now I have both links worked separatly.

Edited:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks

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

RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

RewriteRule ^en/(.*) /$1 [L,R]

This will redirects default language urls directly to site root!
Many thanks.

There is for LigHTTPD:

$HTTP["url"] != "^/en/" {
    url.rewrite-once = (
        "^/.*" => "index.php?/$1",
    )
}
url.redirect = (
    "^/en/.*" => "/$1",
)

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

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

发布评论

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

评论(1

情定在深秋 2024-12-22 17:23:19

这是因为 RewriteRule ^.*$ index.php [NC,L] 正在重写它,并且它永远不会达到您添加的规则。您需要添加一个条件,以便以 /en/ 开头的请求不会被重写到index.php:

# you regular stuff
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Add this condition
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

# Now you can rewrite /en
RewriteRule ^en/(.*) $1 [L,R]

编辑:example.com/en/something 被重定向到example.com/something,然后重写为index.php

It's bceause RewriteRule ^.*$ index.php [NC,L] is rewriting it and it never gets to the rule you added. You need to add a condition so that requests starting with /en/ don't get rewritten to index.php:

# you regular stuff
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Add this condition
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

# Now you can rewrite /en
RewriteRule ^en/(.*) $1 [L,R]

Edited: example.com/en/something gets redirected to example.com/something, then rewritten to index.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文