Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize .htaccess rules?

I have a following .htaccess rules for seo friendly url :

.htaccess rules :

Options -MultiViews
ErrorDocument 404 http://localhost/aponit/dev/not-found.php
ErrorDocument 500 http://localhost/aponit/dev/404.php
RewriteEngine on

RewriteRule ^(?:zones/)?update/(\w+)/?$ zones/update.php?z=$1 [L,QSA,NC]
RewriteRule ^(?:cable-types/)?update/(\w+)/?$ cable-types/update.php?cbl=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Now, using this rules I can access following url :

http://localhost/aponit/dev/zones/update/63
http://localhost/aponit/dev/cable-types/update/3

Now I have to create this type of url many more! So is there anyway in this .htaccess rules to minimize the rules ?

I mean if I need 10 different url I have to add 10 rules to the .htaccess file. e.g:

RewriteRule ^(?:another/)?another-f/(\w+)/?$ another/another-f.php?another-id=$1 [L,QSA,NC]

I want one rules in this.htaccess file for this type of url.

Update:

In index.php file under zones folder I have following code to edit and add new data :

<a class="btn btn-success btn-xs" href="<?php echo SITE_URL."zones/update/$zone_id"?>" >Edit</a> 

<h4 class="panel-title pull-right"><a href="<?php echo SITE_URL.'zones/add' ?>" class="btn btn-primary">Add New Zones</a></h4> 

To edit the form data I need this type of url :

http://localhost/aponit/dev/zones/update/63

Too add data I need this type of url :

http://localhost/aponit/dev/zones/add

and finally in .htaccess rules I need only one rules to follow this type of urls

like image 980
shibbir ahmed Avatar asked Nov 09 '22 11:11

shibbir ahmed


1 Answers

Use these two generic rules instead of above type of rules:

RewriteCond %{DOCUMENT_ROOT}/aponit/dev/$1/$2\.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1/$2.php [L]

RewriteCond %{DOCUMENT_ROOT}/aponit/dev/$1/$2\.php -f
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1/$2.php?q=$3 [L,QSA]

Please note that name of the GET parameter will be same q for all such URLs. Inside .php file you can use $_GET['q'] to read it.

like image 99
anubhava Avatar answered Nov 14 '22 23:11

anubhava