skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Just use this rule:

    RewriteRule ^(.*)/(.*)/(w+)$  /$1/$2.php?z=$3  [QSA,L,NC]
    

    It will rewrite:

    zones/update/63      >>  /zones/update.php?z=63
    cable-types/update/3 >>  /cable-types/update.php?z=63
    

    Note 1: This standardizes your paths. You no longer make the first part (zones/ vs cable-types/ optional)

    Note 2: that you have to standardize the name of your first query parameter. I use z above. Then, within the PHP files, you use it as needed. The cable-types/update.php will use it just as you used the cbl parameter before

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search