V
V
Vlad2016-07-25 16:44:56
Regular Expressions
Vlad, 2016-07-25 16:44:56

How to compose a regular expression for a subcategory URL?

How to correctly write regular expressions for htaccess to open
site.ru/categoria/
site.ru/categoria/podcategoria/

RewriteRule ^([^.]+)/$ index.php?category=$1 [L]
RewriteRule ^([^.]+)/([^.]+)/$ index.php?category=$1&subcategory=$2 [L]

With such a record, the url site.ru/categoria/podcategoria/ creates a GET parameter category = categoria/podcategoria
ALL RIGHT?
RewriteRule ^(.*)/(.*)/$ index.php?category=$1&subcategory=$2 [L,QSA]
RewriteRule ^(.*)/$ index.php?category=$1 [L]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2016-07-26
@shambler81

RewriteCond %{REQUEST_URI} ^categoria/(.*)$ 
# если строка  начинается с categoria
RewriteCond %{REQUEST_URI} !^categoria/podcategoria/(.*)$ 
# защита от цикла ! - отрицание 
RewriteRule ^(categoria/)(.*)$ /$1/podcategoria/$2 [R=301,L] 
# добавит  у всего что есть категория подкатегорию

if you need to manage the get requests themselves, then again, you need a two-level construction of code and rules. in this case, everything will be easier.
I’ll just throw off an example, then you’ll figure it out yourself, if you need a host where you do a redirect to use a get, then escape the questions there, otherwise it won’t work. If you need to remove it, then put it at the end of the question.
# 301 --- http://shop.wildorchid.ru/Catalog/StyleCard.aspx?&sale=1&shop=7&categoryid=2046&style=26778 => http://shop.wildorchid.ru/catalog/nizhnee_bele/1010-defile/
RewriteCond %{QUERY_STRING} (^|&)sale=1($|&)
RewriteCond %{QUERY_STRING} (^|&)shop=7($|&)
RewriteCond %{QUERY_STRING} (^|&)categoryid=2046($|&)
RewriteCond %{QUERY_STRING} (^|&)style=26778($|&)
RewriteRule ^Catalog/StyleCard\.aspx$ /catalog/nizhnee_bele/1010-defile/? [L,R=301]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question