I am trying to implement clean URL with the help of .htaccess in my project.
I got a 404 when i implimented a condition with multiple condition strings like keyword1/keyword2/param
all other conditions like RewriteRule ^home index.php [L,NC] works fine
My file structure be like
/subdirectory/
|-.htaccess
|-index.php
|-edit-user.php
|-new-user.php
my desired clean url is
mysite.com/subdirectory/user/edit/10
and it should translated into
mysite.com/subdirectory/edit-user.php?id=10
Some of the closest solutions i tried so far (but no luck)
RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
RewriteBase /user/
RewriteRule ^user\/edit\/([0-9]+)$ edit-user.php?id=$1 [L,NC]
Any suggestions are highly appreciated.
Since the
.htaccessfile is inside the/subdirectorythen you would need to write the directive like this:And remove any
RewriteBasedirective.\dis simply a shorthand character class for[0-9].The
RewriteRulepattern matches against the relative URL-path (no slash prefix). That is relative to the directory that contains the.htaccessfile. You were also missing the.phpextension on the filename you are rewriting to. You do not need theNCflag unless you really do want to allowed a mixed-case request, but that opens you up to potential "duplicate content" which would need to be resolved in other ways.Actually, you are very close here, but the
RewriteBasedirective would have caused this to fail. The sole purpose of theRewriteBasedirective is to override the directory-prefix that is added back on relative path substitutions. TheRewriteBasedirective sets the "URL-path" (as opposed to filesystem path) that is added back.So, in this example,
RewriteBase /user/would result in the request being rewritten to/user/edit-user.php?id=10(relative to the root), which is clearly wrong based on the file structure you posted.Without the
RewriteBasedefined then the directory-prefix is added back, which results in the rewrite being relative to the directory containing the.htaccessfile.Also, there's no need to backslash-escape slashes since there are no slash delimiters to the regex. (The spaces that surround the argument are the delimiters.)
Careful with this, as this will also match
/homeanythingand/home/somethingetc.