htaccess的 - 如何删除URL重复的单词? [英] .htaccess - how to remove repeated words in url?

查看:127
本文介绍了htaccess的 - 如何删除URL重复的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正则表达式的.htaccess 从这些网址去:

  example.com/hello-world-world
example.com/hello-world-hello
 

这一个:

  example.com/hello-world
 

使用的RewriteCond是这样的:

 的RewriteCond%{REQUEST_METHOD}!= POST
的RewriteCond%{REQUEST_URI} ^正则表达式$
重写规则。 %1 [R = 301,L]
 

解决方案

从塞删除所有重复的话是 pretty的棘手在做的mod_rewrite 只,但这里有一个前瞻根据正则表达式规则比你可以使用:

  RewriteEngine叙述上

的RewriteCond%{REQUEST_URI} ^ /((?:[^  - ] *  - )*)([^  ​​- ] *) - (??(= * \ 2)*)$
重写规则^ /%1%3 [L,R]
 

  • 这将重定向 /你好世界世界 /你好世界
  • 这将重定向 /你好世界问候 /世界你好

更新:按照您的意见:

 的RewriteCond%{REQUEST_METHOD}!= POST
的RewriteCond%{REQUEST_URI} ^(/网站/ VAR1 / VAR2)/((?:[^  - ] *  - )*)([^  ​​- ] *) - (?。?。(= * \ 3)*)$ [NC]
重写规则^%1 /%2%4 [L,R]
 

这将重定向的http://本地主机/网站/ VAR1 / VAR2 /你好世界世界的http://本地主机/网站/ VAR1 / VAR2 /你好世界这个去掉重复的。

更新2:按照您的意见:

 的RewriteCond%{REQUEST_METHOD}!= POST
的RewriteCond%{REQUEST_URI} ^(/网站/ VAR1 / VAR2)/((?:[^ /] * /)*)([^ /] *)/(?。?。(= * \ 3)*)$ [NC]
重写规则^%1 /%2%4 [L,R]
 

这将重定向的http://本地主机/网站/ VAR1 / VAR2 /你好/世界/世界/男孩/男孩 HTTP:/ /本地主机/网站/ VAR1 / VAR2 /你好/世界/男孩

What is the regex in .htaccess to go from those urls:

example.com/hello-world-world
example.com/hello-world-hello

to this one:

example.com/hello-world

using RewriteCond like this:

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^REGEX$
RewriteRule . %1 [R=301,L]

解决方案

Removing all the duplicate words from slug is pretty tricky to be done in mod_rewrite only but here is a lookahead based regex rule than you can use:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/((?:[^-]*-)*)([^-]*)-((?=.*?\2).*)$
RewriteRule ^ /%1%3 [L,R]

  • This will redirect /hello-world-world to /hello-world
  • This will redirect /hello-world-hello to /world-hello

UPDATE: As per your comments:

RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(/site/var1/var2)/((?:[^-]*-)*)([^-]*)-((?=.*?\3).*)$ [NC]
RewriteRule ^ %1/%2%4 [L,R]

This will redirect http://localhost/site/var1/var2/hello-world-world to http://localhost/site/var1/var2/hello-world this removing the duplicate.

UPDATE 2: As per your comments:

RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(/site/var1/var2)/((?:[^/]*/)*)([^/]*)/((?=.*?\3).*)$ [NC]
RewriteRule ^ %1/%2%4 [L,R]

This will redirect http://localhost/site/var1/var2/hello/world/world/boy/boy to http://localhost/site/var1/var2/hello/world/boy

这篇关于htaccess的 - 如何删除URL重复的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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