设置重定向在.htaccess如果URL有一个后缀,可以有一个结束 [英] Set redirect in .htaccess if url has a suffix and can have an end
问题描述
在我的客户分类页面的Virtuemart网站有这样的网址:
On the Virtuemart website of my client category pages have urls like:
example.com/vm-search/level1category-cityname/level2category-cityname
or
example.com/vm-search/level1category-cityname/level2category-cityname/level3category-cityname
or
example.com/vm-search/.../-cityname/result,9-10 (digits may be any numbers from 1 to N)
我需要一个重定向规则的.htaccess,将删除所有citynames上飞除了最后一个城市,也仅剩的结果,9-10
如果它的结尾网址即结果一定是这样的:
I need a redirect rule for .htaccess that would remove all the citynames on the fly except the last city and remaining also result,9-10
if it's the end of the url i.e. results must be like:
example.com/vm-search/level1category/level2category-cityname
or
example.com/vm-search/level1category/level2category/level3category-cityname
or
example.com/vm-search/level1category/level2category-cityname/result,9-10 (digits may be any numbers from 1 to N)
or
example.com/vm-search/level1category/level2category/level3category-cityname/result,9-10 (digits may be any numbers from 1 to N)
请不认为这个问题是 stackoverflow.com/q/30298289/3208225 重复它还有另外一个条件
Please don't view the question as duplicate of stackoverflow.com/q/30298289/3208225 for it has another conditions
推荐答案
您可以使用根的.htaccess这个规则
You can use this rule in root .htaccess
RewriteRule ^(vm-search)/(?:([^-]+)-[^/]+/)+?([^/-]+-(?=[a-z])[^/]+(?:/.*)?)$ /$1/$2/$3 [L,NC,R=302]
编辑:如果您想为避免多次重定向,然后使用ENV这样的变量:
If you want to avoid multiple redirects then use env variable like this:
RewriteRule ^(vm-search)/(?:([^-]+)-[^/]+/)+?([^/-]+-(?=[a-z])[^/]+(?:/.*)?)$ /$1/$2/$3 [NC,L,E=IS_REDIR:1]
RewriteCond %{ENV:REDIRECT_IS_REDIR} =1
RewriteRule . %{REQUEST_URI} [L,R=302]
这篇关于设置重定向在.htaccess如果URL有一个后缀,可以有一个结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!