与普通的前pression htaccess的重写规则 [英] htaccess rewrite rule with regular expression
问题描述
我有一个很难创建重写规则,使用旧的URL的一部分重定向。例如:
旧网址: HTTP:// WWW .example.com的/新闻/ index.php文件/一十一分之二千零一十四/ 07 /我 - 博客 - 后 - 从历史的现场 要么 http://www.example.com/news/index.php /十一分之二千〇一十四/ 07 / my_blog_post_from_old_site
新网址: http://www.example.com/2014/11/07/my -blog岗位
新的URL应该只具有日期和固定链接由破折号剥离后,前三个元素。即使我不知道是否可以使用的.htaccess规则作出规定,但肯定可以使用PHP来完成。
下面是我的.httaccess规则
的RewriteCond%{HTTP_HOST} ^ example.com [NC,OR]
的RewriteCond%{HTTP_HOST} ^ www.example.com [NC]
重写规则^(/?新闻/ index.php文件/.*/[^/]*?)_([^/]*?_[^/]*)$ $ 1- $ 2 [N]
重写规则^(/?新闻/ index.php文件/.*/[^/]*?)_([^/_]*)$ $ 1- $ 2 [R = 301]
重写规则^新闻/ index.php文件/([^/]+)-([^/]+)-(.*)$ http://www.example.com/$1-$2-$3 [L,R = 301 ,NC]
只是匹配块,直到连字符( -
)遇到:
重写规则^新闻/指数\ .PHP /([^-]+-[^-]+-[^-]+).* / $ 1 [R = 301,L ,NC]
I have a hard time to create rewrite rule for a redirect using part of an old URL. Example:
Old URL: http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site or http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
New URL: http://www.example.com/2014/11/07/my-blog-post
New URL should to have only dates and first three elements of a permalink after stripping from dashes. Even I'm not sure if can be done using .htaccess rule but for sure can be done using PHP.
Here are my .httaccess rules
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
RewriteRule ^news/index.php/([^/]+)-([^/]+)-(.*)$ http://www.example.com/$1-$2-$3 [L,R=301,NC]
Just match for the blocks until hyphen (-
) is encountered:
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]
这篇关于与普通的前pression htaccess的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!