rohan_vg
rohan_vg

Reputation: 1129

Htacces rewrite rules and conditions

I have this rule which works perfectly

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

this converts my link from

/demo/index.php?category=ABC&subcategory=XYZ
to
/demo/ABC/XYZ

but at times I want to enter just the category name like this

/demo/ABC/

But unfortunately this gives error Page not found. What causes this error and how can I fix it?

Also can someone provide link to RewriteEngine resources/books/tutorials. I couldn't find anything that would help a beginner like me. Thank you.

Upvotes: 1

Views: 81

Answers (2)

anubhava
anubhava

Reputation: 784878

Change your rule to:

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

This making second argument optional (using * instead of + in [^/]*)

Upvotes: 1

Maggy May
Maggy May

Reputation: 233

Unless /demo/ABC/ is a directory that exists on your local file system, it won't be able to find the page. Your first rule is only rewriting that parameter-filled link into that pseduo-path, it doesn't establish a virtual directory or create a solid redirect.

The best info for RewriteRule is probably going to be in the actual Apache docs.

Upvotes: 0

Related Questions