.htacess rewrite from view/index.php?id=123 to /view/123

34 Views Asked by At

I have a directory named view. Within there I have index.php.

Currently, users have to go to mydomain.com/account/new/prescriptions/view/index.php?id=61312 to view the data.

How can i use .htaccess to change this to mydomain.com/account/new/prescriptions/view/61312

I have tried the below:

# Specific rewrite for account/new/prescriptions/view
RewriteRule ^(account/new/prescriptions/view)/(\d+)$ $1 [END]

# Append .php extension for other requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]

However, this changes the url to from index.php?id=61312 to index?id=61312

1

There are 1 best solutions below

2
anubhava On

You may use this code inside view/.htaccess:

RewriteEngine On

# rewrite for /path/123 to /path/index.php?id=123
RewriteRule ^(\d+)/?$ index.php?id=$1 [END,QSA]

# Append .php extension for other requests
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [END]