重写单和连续的下划线破折号 [英] Rewrite single and consecutive underscores to dashes

查看:162
本文介绍了重写单和连续的下划线破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写用下划线的URL中短线的购物网站。

I'm trying to rewrite URLs with underscores in to dashes for a shopping site.

产品名称目前有下划线的空间。一个典型的URL如下:

Product names currently have underscores for spaces. A typical URL is as follows:

HTTP://test.local/category-name/product_name_a

我目前的.htaccess如下,并成功地重写此为:

My current .htaccess is as follows and successfully rewrites this to:

HTTP://test.local/category-name/product-name-a

RewriteEngine On
RewriteBase /

#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_+(.+)$ $1-$2 [L]

# Redirect to index.php & standard Opencart stuff
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

然而,一些产品已经连续下划线他们如

However some products have consecutive underscores in them such as

HTTP://test.local/category-name/product__name__b

或的混合物在两个如:

HTTP://test.local/category-name/product__name_a

我已经试过,但让这些改写为:

I've tried and failed to get these to rewrite to:

HTTP://test.local/category-name/product--name --b

HTTP://test.local/category-name/product--name-一个

在不破坏现有的重写。任何帮助非常AP preciated。

Without breaking the existing rewrites. Any help much appreciated.

推荐答案

只需调整你的正则表达式中的第二个规则:

Just tweak your regex in 2nd rule:

RewriteEngine On
RewriteBase /

#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [L,R=302,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_(.+)$ $1-$2 [L]

# Redirect to index.php & standard Opencart stuff
RewriteRule ^sitemap\.xml$ index.php?route=feed/google_sitemap [L]

RewriteRule ^googlebase\.xml$ index.php?route=feed/google_base [L]

RewriteRule ^download/(.*) /index.php?route=error/not_found [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

这篇关于重写单和连续的下划线破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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