Mustafa
Mustafa

Reputation: 825

htaccess RewriteRule one parameter includes other

I encountered an issue for the first time and have no clue why i am having this. Below you see three lines of my .htaccess file. While /urun/test-123 using urundetay.php, /urunlerimiz/ have to use urunlerimiz.php. The issue is since 'urunlerimiz' include 'urun' inside of it, all of those three links are using urundetay.php.

RewriteRule ^urunlerimiz/?(.*)$ urunlerimiz.php [NC]
RewriteRule ^urun/?(.*)$ urundetay.php [NC]
RewriteRule ^urun-kategori/?(.*)$ urundetay.php [NC]

It works correctly when i set those parameters completely different. But i have to use it that way and i want to learn. What is the solution for this?

Upvotes: 1

Views: 174

Answers (2)

Vicent
Vicent

Reputation: 5452

You need to use negative lookaheads in your pattern so that the string lerimiz doesn't produce a match. As, accordingly to the RewriteRule docs the regex pattern used by RewriteRule is Perl compatible you should be able do it this way:

RewriteRule ^urun(?!lerimiz)/?(.*)$ urundetay.php [NC]

Upvotes: 1

anubhava
anubhava

Reputation: 785186

I have experienced this problem in one of my project as well and here is how I resolved it:

RewriteRule ^urunlerimiz(/.*|)$ urunlerimiz.php [NC,L]
RewriteRule ^urun(/.*|)$ urundetay.php [NC,L]
RewriteRule ^urun-kategor(/.*|)$ urundetay.php [NC,L]

Trick is to append (/.*|) after every keyword.

e.g. for regex ^urun(/.*|)$ this will match /urun OR /urun/ OR /urun/foo but will not be matching /urunlerimiz and /urun-kategor

Upvotes: 1

Related Questions