I am trying to understand how .htaccess redirects work.
Say for instance, my user navigates to:
www.website.com/users/bob
How can I redirect that user to:
www.website.com/users.php?user=bob
I am trying to understand how .htaccess redirects work.
Say for instance, my user navigates to:
www.website.com/users/bob
How can I redirect that user to:
www.website.com/users.php?user=bob
Your .htaccess would look something like this:
# turn on the engine
RewriteEngine On
RewriteRule ^users/(.*)$ /users.php?user=$1
# Pattern:
# ^ = start of URL (.htaccess doesn't get the leading "/")
# users/ = your path
# (.*) = capture any character, any number of times
# $ = end of string
# Result:
# /users.php?user= = your path
# $1 = the first group captured in the pattern
I recommend you to read the technical details of mod_rewrite. There you get a good insight into the internal processing.
For your specific example, a suitable rule might look like this:
RewriteRule ^users/([a-z]+)$ users.php?user=$1
Now when /users/bob
is requested, mod_rewrite will:
/
if the .htaccess file is located in the document root) from the requested pathusers/bob
) against the patterns until one matches
RewriteCond
directives they, would be tested too$
n
and %
n
with the corresponding matches), if the pattern matches and the conditions are evaluated to true/
) or absolute URL (beginning with the URL scheme)