ExpressionEngine mod_rewrite 规则将带下划线的 URL 重定向为破折号
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一套完整的 RewriteRules,可以满足您的需要:
让您的
mod_rewrite
规则忽略 ExpressionEnginesystem
文件夹,并且不将下划线_
替换为破折号-
使用以下内容:RewriteRule ^(images|themes|system) - [L,NC]
将 RewriteRule 剖析成简单的英语
-
标志指示 Apache 不执行任何操作,并且不重写 URIL
标志表示这应该是最后一个规则;忽略后面的所有内容NC
标志表示不区分大小写(因此“System”或“SYSTEM”也匹配)此“忽略”规则特别重要,您可能需要添加其他目录以排除,具体取决于你的目录结构。
否则,您最终可能会得到用下划线保存的图像和其他文件,这些文件被破折号替换。
注意:如果您的 URL 包含三个以上下划线,则需要在每个 要替换的 URL 标题的单词分隔符:
Here's a complete set of RewriteRules that should do what you need:
To have your
mod_rewrite
rules ignore the ExpressionEnginesystem
folder and not replace underscores_
with dashes-
use the following:RewriteRule ^(images|themes|system) - [L,NC]
Dissecting the RewriteRule into plain English:
-
flag instructions Apache to do nothing, and to not rewrite the URIL
flags means this should be last rule; ignore everything followingNC
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:
您在替换字符串中包含了“index.php”。
You included 'index.php' in your replacement string.