与普通的前pression htaccess的重写规则重写、规则、普通、htaccess

2023-09-02 00:36:37 作者:夜幕篱下浅笙歌°

我有一个很难创建重写规则,使用旧的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]
 
动态代理 AOP ,这次包教包会 www 的博客 CSDN博客

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]