shibbir ahmed shibbir ahmed - 4 months ago 21
PHP Question

How to change query string url using .htaccess rules?

I have following file in root directory :

Dev (root)
update.php
zones.php


This zones.php file is showing using following url:

http://localhost/aponit/dev/zones (I hide .php extension using .htaccess rules)


In zones.php file I have a edit link to edit a form via query string. The link is bellow :

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


When I click on this link it's showing following url :

http://localhost/aponit/dev/zones/update?z=55


But in browser it's showing me error message :

Internal Server Error

Because my .htaccess rules is not define appropriate rules for that desired link.

What I want now :

I want the url should be user friendly. E.g

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


How can I create this link using .htaccess ?

Current .htaccess rules :

ErrorDocument 404 /not-found.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Answer

You can have another rule to handle zones/update/55:

Options -MultiViews
ErrorDocument 404 /not-found.php
RewriteEngine on

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

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