mod_rewrite-重定向到root并用破折号替换下划线 [英] mod_rewrite - redirect to root and replace underscores with dashes

查看:87
本文介绍了mod_rewrite-重定向到root并用破折号替换下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦恼了几个小时-试图掌握mnod_rewrite的语法,基本上,我正在迁移一个网站,以使URL像这样:

I have been agonising over this for a few hours - trying to get to grips with the syntax for mnod_rewrite, Basically, I am migrating a site so that urls like:

http://www.somedomain.co.uk/news/article/a_blog_post

成为

http://www.somedomain.co.uk/a-blog-post

此处的两个关键是删除 / news / article位并用短划线代替下划线。 / p>

The two key things here are removing the "/news/article" bit and replacing underscores with dashes.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/?news/article$
RewriteRule ^([^_]*)_([^_]*_.*)$ $1-$2 [N]

RewriteCond %{REQUEST_URI} ^/news
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [N]

RewriteRule ^news/article/(.*)$ /$1 [L,R=301]

我似乎无法启动RewriteCond。

I can't seem to get the RewriteCond to fire. Please help!

推荐答案

您可以这样做:

RewriteEngine On

# remove /news/article/ when there is no _ left
RewriteRule ^news/article/([^_]+)$ /$1 [L,R=301,NC,NE]

# use recursion based rule to repalce _ by -
RewriteRule ^(news/article/[^_]*)_+(.*)$ $1-$2 [N,DPI]

这篇关于mod_rewrite-重定向到root并用破折号替换下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆