将单个和连续的下划线重写为破折号
Rewrite single and consecutive underscores to dashes
我正在尝试将带有下划线的 URLs 重写为购物网站的破折号。
产品名称目前使用下划线表示空格。一个典型的URL如下:
http://test.local/category-name/product_name_a
我当前的.htaccess如下,成功改写为:
http://test.local/category-name/product-name-a
RewriteEngine On
RewriteBase /
#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ - [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_+(.+)$ - [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_= [L,QSA]
但有些产品中有连续的下划线,例如
http://test.local/category-name/product__name__b
或两者的混合,例如:
http://test.local/category-name/product__name_a
我试过但未能将这些重写为:
http://test.local/category-name/product--name--b
http://test.local/category-name/product--name-a
不破坏现有的重写。非常感谢任何帮助。
只需调整第二条规则中的正则表达式即可:
RewriteEngine On
RewriteBase /
#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ - [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_(.+)$ - [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_= [L,QSA]
我正在尝试将带有下划线的 URLs 重写为购物网站的破折号。
产品名称目前使用下划线表示空格。一个典型的URL如下:
http://test.local/category-name/product_name_a
我当前的.htaccess如下,成功改写为:
http://test.local/category-name/product-name-a
RewriteEngine On
RewriteBase /
#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ - [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_+(.+)$ - [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_= [L,QSA]
但有些产品中有连续的下划线,例如
http://test.local/category-name/product__name__b
或两者的混合,例如:
http://test.local/category-name/product__name_a
我试过但未能将这些重写为:
http://test.local/category-name/product--name--b
http://test.local/category-name/product--name-a
不破坏现有的重写。非常感谢任何帮助。
只需调整第二条规则中的正则表达式即可:
RewriteEngine On
RewriteBase /
#Rewrite Underscores to dashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_([^_]*)$ - [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^_]*)_(.+)$ - [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_= [L,QSA]