Marek Czyż
Marek Czyż

Reputation: 199

how to apply a rule in .htaccess

I have to create a condition for redirecting a website to other URL and I think it is quite complicated condition.

#RewriteRule (pl)?(/page/5/?)$  /polityka-prywatnosci/ [R=301,L]

That's my base for this redirect, but I need to exclude some words preceding /page/5/ for example word 'car' to not execute this redirect when this word appears. To make everything clear: when Ive got: http://some-url/page/5 I want to redirect this to http://some-url/polityka-prywatnosci/ but when there is http://some-url/car/page/5 i want to stay in this URL. I tried such trick as: #RewriteRule (pl)?[^(car)](/page/5/?)$ /polityka-prywatnosci/ [R=301,L] but it doesnt work. What are the possibilities to overcome it?

Upvotes: 2

Views: 46

Answers (1)

logi-kal
logi-kal

Reputation: 7880

If negative lookbehind is allowed, you can try with:

RewriteRule (?<!car|car/pl)(/pl)?(/page/5/?)$  /polityka-prywatnosci/ [R=301,L]

See regex demo.

Another way could be to state 3 consecutive replacements:

RewriteRule (/page/5/?)$  /polityka-prywatnosci/ [R=301,L]
RewriteRule (car|car/pl)/polityka-prywatnosci/$  $1/page/5/ [R=301,L]
RewriteRule /pl(/polityka-prywatnosci/)$  $1 [R=301,L]

Upvotes: 1

Related Questions