htaccess重写RegEx在参数之间添加静态值

时间:2018-05-22 09:26:40

标签: .htaccess url mod-rewrite

我试图用htaccess重写网址。请求网址长度是可变的。 它由以下内容组成:

的http:// [domain.com] / [COUNTRYCODE] / [主要的类别] / [亚类别] / [子子类 / [产品名称] html的

当然它也可以减少类别。

另一种选择只是产品页面,例如: HTTP:// [domain.com] / [COUNTRYCODE] / [产品名称] html的

我想重写这些网址,并在[countrycode]和网址的其余部分之间添加一个静态值。另外,我想在每个重写的URL的末尾将.html更改为.aspx。

我只能使用以下主要类别重写网址: 这个htaccess是http://example.com/en/main-category

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(.*)example\.com [NC]
RewriteRule ^(.*)/(.*)$ https://example.com/$1/3/$2.aspx [R=301]

但是如果网址包含一个尾部斜杠或更多类别,静态值(" 3")会被添加到错误的位置。

要清楚,我想要这个结构:

请求:http://example.com/en/main-category/sub-category/product-name.html

重写:https://example.com/en/3/main-category/sub-category/product-name.aspx

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以在站点根目录中使用此规则.htaccess:

RewriteEngine On

RewriteRule ^([a-z]{2})/(?!3/)(.+)\.html$ https://%{HTTP_HOST}/$1/3/$2.aspx [L,NC,NE,R=301]