I have an URL that Looks like:
https://www.example.com/project/api/read.php?uid=1234567&dvc=ABCDE
I would like to establish a rewrite rule that turns the URL in:
https://www.example.com/project/api/read/1234567/ABCDE
My try Looks like this:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
You are missing a slash between the path segments in the RewriteRule
pattern.
Assuming your .htaccess
file is located inside the /project/api
subdirectory (as implied by your directive) then try the following instead:
# MultiViews must be disabled for the rewrite to work
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteRule ^read/([\w-]+)/([\w-]*)$ read.php?uid=$1&dvc=$2 [L,QSA]
\w
is a shorthand character class that is the same as [0-9a-zA-Z_]
, to which we add the hyphen (-
).
Since the URL /read
maps directly to the file /read.php
MultiViews must be disabled, otherwise mod_negotiation "rewrites" the request to /read.php
(before mod_rewrite) without any URL parameters.
Note that I changed the quantifier from *
to +
in the first path segment, since this would seem to be mandatory in your URL?
Your example URL (/read/1234567/ABCDE
) shows digits in the first path segment and letters in the second. If this is an accurate reflection of the type of URL then the regex should be made more restrictive to check just for this data type.
Aside:
RewriteEngine On # Turn on the rewriting engine
Note that Apache does not support line-end comments. This may be "OK" in this instance because the text after RewriteEngine On
is simply ignored. In other cases you might get a 500 internal server error.