LibertyTech
LibertyTech

Reputation: 25

How to Rewrite url using RewriteRule

I could not figure out why the rewrite rule is not working. I want to rewrite localhost/admin/update.php?id=1 where id is dynamic based on the user I clicked to fetch the data from the database to localhost/admin/update/1

This is my attempted .htaccess file:

RewriteEngine on
RewriteRule ^update update.php [NC,L]
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]

Upvotes: 0

Views: 69

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteRule ^update update.php [NC,L]
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]

You need to remove the first rule and ensure that MultiViews is disabled. Your first rule is blocking the second rule (and serves no purpose from the criteria stated in the question). For example:

Options -MultiViews

RewriteEngine On
RewriteRule ^update/([0-9]+)$ update.php?id=$1 [NC,L]

This assumes the .htaccess file is located inside the /admin subdirectory (as implied in comments).

I want to rewrite localhost/admin/update.php?id=1 where id is dynamic based on the user I clicked to fetch the data from the database to localhost/admin/update/1

Note that your description is completely the opposite of what the above directive does (and what you should be doing). This rule rewrites the request from /admin/update/1 to /admin/update.php?id=1. You should be linking to /admin/update/1 in your application.

Upvotes: 3

Related Questions