D
D
Dmitry2020-10-20 20:49:28
htaccess
Dmitry, 2020-10-20 20:49:28

Problem with RewriteRule in htaccess?

Friends, please tell me how to correctly compose RewriteRule so that everything works correctly.
There is a rule:

RewriteRule ^katalog/([a-z0-9-]+)(/([a-z0-9-]+))?(/page-(\d+))?/$ /category.php?cat1=$1&cat2=$3&page=$5 [L,QSA]


How to write an exception in the form of page-(\d+) for the second (cat2=$3) regular expression? Just if the url is: site.ru/katalog/aminokisloty/aakg/page-2/ then everything works. And if there is no subcategory: site.ru/katalog/aminokisloty/page-2/ then of course not, because it passes page to (cat2=$3).

Of course, I can simply remove / from the pages and then this will solve the issue, but somehow I don’t want to, can I do it 100%?
Thank you very much in advance!

PS subcategories can have numbers, ie. it's just not possible like this - ^katalog/([a-z0-9-]+)(/([az-]+))?(/page-(\d+))?/$

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-10-20
@Deimos

By default, quantifiers ? * +are greedy.
By adding ?them, you can switch to lazy mode?? *? +?

RewriteRule ^katalog/([a-z0-9-]+)(/([a-z0-9-]+))??(/page-(\d+))?/$ /category.php?cat1=$1&cat2=$3&page=$5 [L,QSA]

In an expression, ( а | b )alternatives are evaluated from left to right.
So you (/([a-z0-9-]+))??can replace(|/([a-z0-9-]+))
RewriteRule ^katalog/([a-z0-9-]+)(|/([a-z0-9-]+))(/page-(\d+))?/$ /category.php?cat1=$1&cat2=$3&page=$5 [L,QSA]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question