xtr3mist
xtr3mist

Reputation: 63

.htaccess get value after last slash

My code:

RewriteCond %{REQUEST_URI}  /category/.*/([^/]+)/?     [NC]
RewriteCond %{REQUEST_URI}  !category\.php      [NC]
RewriteRule  .*/category.php?cat=%1 [L,NC,QSA]

I want to get last word from URL as $_GET['cat'], and it works, but when I put in URL only one or two slash then it doesn't work.

https://example.com/category - don't work
https://example.com/category/motorization - don't work
https://example.com/category/motorization/cars - work
https://example.com/category/motorization/cars/audi -work

Upvotes: 1

Views: 460

Answers (1)

anubhava
anubhava

Reputation: 786329

You can use this rule to capture anything after /category/ in $1:

Options -MultiViews
RewriteEngine On

RewriteRule ^/?category(?:/(?:.*/)?([^/]+))?/?$ category.php?cat=$1 [L,NC,QSA]

Upvotes: 1

Related Questions