1
votes

I'm trying to rewrite url's with htaccess. The request url length is variable. It consists of the following:

http://[domain.com]/[countrycode]/[main-category]/[sub-category]/[sub-sub-category]/[product-name].html

Of course it could also have less categories.

Another option is only a product page, like: http://[domain.com]/[countrycode]/[product-name].html

I want to rewrite these url's and add a static value between the [countrycode] and the rest of the url path. Also, I want to change .html to .aspx at the end of every rewritten URL.

I came as far as being able to rewrite a url with only the main category like: http://example.com/en/main-category with this htaccess:

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]

But if the url contains a trailing slash or more categories, the static value ("3") gets added in the wrong place.

to be clear, I want this structure:

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

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

Thanks for your help!

1

1 Answers

1
votes

You may use this rule in site root .htaccess:

RewriteEngine On

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