当 Drupal Clean URL 启用时,如何重定向特定的 URL 模式?

发布于 2024-10-05 16:21:25 字数 979 浏览 0 评论 0原文

我使用带有 Apache 和 mod_rewrite 模块的干净 URL 安装了 Drupal 5.23。我使用 .htaccess 文件来实现干净的 URL 功能,配置如下:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

我将禁用网站上的本地化/国际化插件,这将更改网站上每个页面的 URL网站从 http://www.example.com/en/url-to-a-pagehttp://www.example.com/url-to-a-page< /code> (/en 部分被删除)。

我想添加 mod_rewrite 规则,为 URL 中包含 /en 部分的任何传入 URL 提供 HTTP 301 重定向响应,以便将它们定向到正确的页面。

我尝试将以下几行添加到我的 .htaccess 文件中,位于现有规则之上和之下,但在这两种情况下,访问带有 /en 的页面都会导致 HTTP 404未找到回复:

RewriteRule ^en/(.+)$ http://www.example.com/$1 [R=301]

如果我注释掉现有规则,我的规则就可以正常工作。我还尝试向规则添加条件,但这似乎也没有效果:

RewriteCond %{REQUEST_URI} =/en/*

I have a Drupal 5.23 installation using clean URLs with Apache and the mod_rewrite module. I am using an .htaccess file for the clean URLs functionality with the following configuration:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

I am going to be disabling the Localization/Internationalization plugins on the website, which is going to change every single page's URL on the website from http://www.example.com/en/url-to-a-page to http://www.example.com/url-to-a-page (the /en portion is being stripped out).

I would like to add a mod_rewrite rule to give an HTTP 301 Redirect response for any incoming URLs with the /en portion in the URL so they are directed to the correct page.

I've tried adding the following lines to my .htaccess file both above and below the existing rules, but in both cases visiting a page with /en results in an HTTP 404 Not Found response:

RewriteRule ^en/(.+)$ http://www.example.com/$1 [R=301]

If I comment out the existing rules, my rule works just fine. I've also tried to add a condition to the rule, but this doesn't appear to have an effect either:

RewriteCond %{REQUEST_URI} =/en/*

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-10-12 16:21:25

在编写所有自定义重定向时,我遇到了这个问题,结果证明解决方案是在重定向行中添加一个“L”。请尝试执行以下操作:

RewriteRule ^en/(.+)$ http://www.example.com/$1 [L,R=301]

注意行尾附近的“L”。根据 Apache RewriteRule 文档,这意味着“停止此处重写过程,并且不再应用任何重写规则”。

This came up for me when writing all of my custom redirects, and it turns out the solution was to add an "L" to the redirect line. Give the following at try:

RewriteRule ^en/(.+)$ http://www.example.com/$1 [L,R=301]

Note the "L" near the end of the line. That, according to the Apache RewriteRule docs, means "Stop the rewriting process here and don't apply any more rewrite rules".

满栀 2024-10-12 16:21:25

除了sillygwailo的建议之外,我建议您确保您的RewriteCond(我认为需要)实际上匹配..

来自apache文档:

=CondPattern'(按字典顺序相等)
将 CondPattern 视为普通字符串,并按字典顺序将其与 TestString 进行比较。如果 TestString 按字典顺序等于 CondPattern,则为 True(这两个字符串完全相等,逐个字符)。如果 CondPattern 为“”(两个引号),则将 TestString 与空字符串进行比较。

那么,它可能只匹配包含实际“*”的 URL..?不确定,但你也可以尝试这个:

RewriteCond %{REQUEST_URI} ^/en/.*

In addition to what sillygwailo suggest, I'd recommend you to make sure that your RewriteCond (needed, I think) actually matches..

from the apache docs:

=CondPattern' (lexicographically equal)
Treats the CondPattern as a plain string and compares it lexicographically to TestString. True if TestString is lexicographically equal to CondPattern (the two strings are exactly equal, character for character). If CondPattern is "" (two quotation marks) this compares TestString to the empty string.

So, It could possibly match only an URL containing an actual '*'..? Not sure, but you could also try this:

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