Gedmi
Gedmi

Reputation: 33

.htaccess - universal rule

this is my first question on this site. Sorry for any mistakes and etc. I have tried to search for a similar topic, but i didn't found anything.

Example: .htaccess file (just a little part of):

# Rules for admin meniu 
RewriteRule ^adm/([a-z]+)/([0-9]+)?/?([a-zA-Z0-9]+)?/?$ index.php?page=adm&page_id=$1&id=$2&act=$3 [NC,L]

This one rule fits few subcategories of admin panel. As we can see I have 1 static and 2 optional variables. The static one is for switch($_GET[page_id]) on my admin.php. Second variable (optional) is for some id (News, Users, Projects or smth..). Third variable (also optional) is for action (delete, edit, ban and so on..). So, if I want to check user's information i am using this link:

index.php?page=adm&page_id=[users_page]&id=[users_id]

and I don't need the action. But if i want to check some news:

index.php?page=adm&page_id=[news_page]&id=[users_id]&act=[edit]

But sometimes i just need to get to subcategory:

index.php?page=adm&page_id=[news_page]

And finally...

Question: Is this a good idea to make this rewrite rule universal for couple of admin panel's categories or I should do this separately for each one. Or there is some other solutions? Keeping in mind that some parts of that link is not using. Sometimes I don't need &act=... or sometimes I just need only &page_id=... And there are a lot of situations like this one. Hope u will understand this.

Upvotes: 3

Views: 104

Answers (1)

anubhava
anubhava

Reputation: 785491

Why not keep it clean and simple and have 3 separate rules to handle various cases:

RewriteEngine On

RewriteRule ^adm/([a-z]+)/([0-9]+)/([a-z0-9]+)/?$ index.php?page=adm&page_id=$1&id=$2&act=$3 [NC,L,QSA]

RewriteRule ^adm/([a-z]+)/([0-9]+)/?$ index.php?page=adm&page_id=$1&id=$2 [NC,L,QSA]

RewriteRule ^adm/([a-z]+)/?$ index.php?page=adm&page_id=$1 [NC,L,QSA]

Upvotes: 1

Related Questions