在Opencart中,Mod_rewrite下划线(或连字符)

我正在将一个电子商务网站转移到一个新的购物篮。 由于某种原因,旧网站使用破折号作为类别,但是对于产品却强调了下划线。

因此,产品的完整url如下所示:

http://www.example.com/Engineering-Common-Bricks/65mm_Class_B__Solid_Engineering_Brick__Price_Each 

新的篮子使用破折号的一切,所以我需要重写传入的url,如上面的以下内容:

 http://www.example.com/Engineering-Common-Bricks/65mm-Class-B--Solid-Engineering-Brick--Price-Each 

我知道有很多重写下划线的材料,但是他们似乎没有为我工作。

我正在使用Opencart,它带有一组现有的重写规则,所以这些可能会干扰我正在添加的新规则。

现有的.htaccess如下所示:

 Options +FollowSymlinks # Prevent Directoy listing Options -Indexes # Prevent Direct Access to files <FilesMatch "\.(tpl|ini|log)"> Order deny,allow Deny from all </FilesMatch> # SEO URL Settings RewriteEngine On # If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ RewriteBase / 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] 

任何帮助或指导将受到感谢。 谢谢。

根据Esa对以下内容的回答更新.htaccess:

选项+ FollowSymlinks选项 – 索引

 <FilesMatch "\.(tpl|ini|log)"> Order deny,allow Deny from all </FilesMatch> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N] RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] RewriteBase / 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] 

正常工作:

http://example.com/this_is_a_category

但是导致Apache崩溃:

http://example.com/this_is_a_category/this_is_a_product

由于RewriteRules用于正则expression式匹配而不是replace,所以在第一行

 RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N] RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] 

不重复。 因此,对于每一个下划线,你将需要一行

 RewriteRule ^(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5-$6-$7 [R=301,L] RewriteRule ^(.*)_(.*)_(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5-$6 [R=301,L] RewriteRule ^(.*)_(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [R=301,L] RewriteRule ^(.*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4 [R=301,L] RewriteRule ^(.*)_(.*)_(.*)$ /$1-$2-$3 [R=301,L] RewriteRule ^(.*)_(.*)$ /$1-$2 [R=301,L] 

replace1-5下划线,导致

 /Engineering_Common_Bricks/65mm_Class_B-Solid-Engineering-Brick-Price-Each 

所以对于9个替代,你需要更多的几行到顶部。 模式将是相同的,但更长的expression式需要成为第一。

但是,这将用短划线replace所有下划线(并在最后一个之后redirect),所以您不能有任何包含下划线的文件名。 您也可以通过在这些规则之前添加RewriteCond指令来防止这种情况的发生:

 RewriteCond %{REQUEST_FILENAME} !-f