.htaccess - URL Rewrite GET parameters -
i want url following
www.website.com/home&foo=bar&hello=world
i want first parameter change
however actual "behind scenes" url this
www.website.com/index.php?page=home&foo=bar&hello=world
all tutorials find change of parameters.
any appreciated!
add .htaccess
in web root /
directory
rewriteengine on rewritebase / rewriterule ^home$ index.php?page=home&%{query_string} [nc,l]
if want work pages i.e. /any-page
gets served index.php?page=any-page
use
rewriteengine on rewritebase / rewritecond %{request_filename} !-d # not dir rewritecond %{request_filename} !-f # not file rewriterule ^(.*)$ index.php?page=$1&%{query_string} [nc,l]
how these rules work?
a rewriterule
has following syntax
rewriterule [pattern] [substitution] [flags]
the pattern can use regular expression , matched against part of url after hostname , port (with .htaccess
placed in root dir), before query string.
first rule
the pattern ^home$
makes first rule match incoming url www.website.com/home
. %{query_string}
captures , appends after /home?
internally substituted url index.php?page=home
.
the flag nc
makes rule non case-sensitive, matches /home
or /home
well. , l
marks last i.e. rewriting should stop here in case there other rules defined below.
second rule
this one's more generic i.e. if of site pages follow url pattern instead of writing several rules, 1 each page, use generic one.
the .*
in pattern ^(.*)$
matches /any-page-name
, parentheses capture any-page-name
part $1
variable used in substitution url index.php?page=$1
. &
in page=home&
, page=$1&
separator used between multiple query string field-value pairs.
finally, %{query_string}
, [nc,l]
flags work same in rule one.
Comments
Post a Comment