ExpressionEngine mod_rewrite 规则将带下划线的 URL 重定向为破折号

发布于 2024-12-17 22:56:55 字数 1579 浏览 0 评论 0原文

我使用 ExpressionEngine 作为我的 CMS,并希望从我的网站 URL 中删除下划线并将其替换为破折号。

例如,我有一个格式如下的 URL:

http://example.com/index.php/menu/friday-lunch

要从 URL 中删除 index.php,我使用以下 mod_rewrite 规则:

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

这有效,因为我只需输入: http://example.com/menu/friday-lunch

在旧网站上我使用下划线代替页面的连字符数URI,因此我编写了一个 mod_rewrite 规则来重定向带有下划线的 URI 以使用破折号。

因此,使用以下 RewriteRule,friday_lunch 变为 friday-lunch

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

该规则效果很好,只是它 301 重定向到 example.com/index.php/menu/ friday-lunch 而不是 example.com/menu/friday-lunch — 请注意添加了 index.php

以下是我当前使用的整个 .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Removes index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
    RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] 
</IfModule>

How can I redirect all of my URLs with underscores to equal with dashes?

额外提示:更糟糕的是,不得使用连字符重写指向 /system 的 URL,例如:example.com/system/login_in/

I'm using ExpressionEngine as my CMS and would like to remove underscores from my site's URLs and replace them with dashes.

For example, I've got a URL that is formatted like this:

http://example.com/index.php/menu/friday-lunch

To remove index.php from the URL, I'm using the following mod_rewrite rule:

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Which works, since I can just type in: http://example.com/menu/friday-lunch

On the old site I used underscores instead of hyphens for page URIs, so I wrote a mod_rewrite rule to to redirect URIs with underscores to use dashes.

So friday_lunch becomes friday-lunch using the following RewriteRule:

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

This rule works rather well, except that it 301 Redirects to example.com/index.php/menu/friday-lunch instead of example.com/menu/friday-lunch — notice the addition of index.php.

Here's the entire .htaccess I'm currently using:

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Removes index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
    RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] 
</IfModule>

How can I redirect all of my URLs with underscores to the equivalent with dashes?

Bonus: to make matters worse, URLs that lead to /system, must not be rewritten with a hyphen, e.g.: example.com/system/login_in/.

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

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

发布评论

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

评论(2

北城挽邺 2024-12-24 22:56:55

这是一套完整的 RewriteRules,可以满足您的需要:

<IfModule mod_rewrite.c>
    # Enable Apache's RewriteEngine
    RewriteEngine On

    # Ignore Matching Directories
    RewriteRule ^(images|themes|system) - [L,NC]

    # Replace Underscores with Dashes
    RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
    RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]

    # Remove index.php from ExpressionEngine URLs
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

让您的 mod_rewrite 规则忽略 ExpressionEngine system 文件夹,并且不将下划线 _ 替换为破折号 - 使用以下内容:

RewriteRule ^(images|themes|system) - [L,NC]

将 RewriteRule 剖析成简单的英语

  • - 标志指示 Apache 不执行任何操作,并且不重写 URI
  • L 标志表示这应该是最后一个规则;忽略后面的所有内容
  • NC 标志表示不区分大小写(因此“System”或“SYSTEM”也匹配)

此“忽略”规则特别重要,您可能需要添加其他目录以排除,具体取决于你的目录结构。

否则,您最终可能会得到用下划线保存的图像和其他文件,这些文件被破折号替换。


注意:如果您的 URL 包含三个以上下划线,则需要在每个 要替换的 URL 标题的单词分隔符

RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)$ /$1-$2-$3-$4 [R=301,L]

Here's a complete set of RewriteRules that should do what you need:

<IfModule mod_rewrite.c>
    # Enable Apache's RewriteEngine
    RewriteEngine On

    # Ignore Matching Directories
    RewriteRule ^(images|themes|system) - [L,NC]

    # Replace Underscores with Dashes
    RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
    RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]

    # Remove index.php from ExpressionEngine URLs
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

To have your mod_rewrite rules ignore the ExpressionEngine system folder and not replace underscores _ with dashes - use the following:

RewriteRule ^(images|themes|system) - [L,NC]

Dissecting the RewriteRule into plain English:

  • The - flag instructions Apache to do nothing, and to not rewrite the URI
  • The L flags means this should be last rule; ignore everything following
  • The NC flag means no-case (so "System" or "SYSTEM" is also matched)

This "ignore" rule is especially important and you may need to add additional directories to exclude depending on your directory structure.

Otherwise, you may end up with images and other files saved with underscores that get replaced with dashes.


Note: If your URLs contain more than three underscores, you'll need to add another RewriteRule above the existing ones for each Word Separator for URL Titles you want to replace:

RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)$ /$1-$2-$3-$4 [R=301,L]
绮筵 2024-12-24 22:56:55

您在替换字符串中包含了“index.php”。

RewriteRule ^(.*)$ index.php/$1 -> RewriteRule ^(.*)$ $1

You included 'index.php' in your replacement string.

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