php - mod_rewrite for dynamically mapping SEO friendly links directly to individual files, without routing via front controller -
i'm looking seo friendly url rewrite rule work common php site doesn't have front controller. map seo friendly url directly php file found exist on server , convert remaining url branches standard url parameters.
for example:
/folder1/folder2/folder3/page/var1/val1/var2/val2/var3/val3
would map to:
/folder1/folder2/folder3/page.php?var1=val1&var2=val2&var3=val3
now, here's tricky part. since rewrite rules need agnostic names of folders, pages, , variables, need base rewrite of url parameters on exact location along link can found file exists along path. instance, consider if following file happened exist (hypothetically) off document root: /folder1/folder2.php
in case, following remapping legitimate , acceptable:
/folder1/folder2.php?folder3=page&var1=val1&var2=val2&var3=val3
this ultimate rewrite rule many traditional websites have been built want urls , parameters instantly become url-friendly.
the examples have found involve mapping work single front controller or otherwise hard-coded files in rule expected exist rather have mod_rewrite detect existence dynamically. they're related, not flexible file found exist:
the apache web server know such concept:
-
the effect of
multiviews
follows: if server receives request/some/dir/foo
, if/some/dir
hasmultiviews
enabled, ,/some/dir/foo
not exist, server reads directory looking files named foo.*, , fakes type map names files, assigning them same media types , content-encodings have if client had asked 1 of them name. chooses best match client's requirements. -
this directive controls whether requests contain trailing pathname information follows actual filename (or non-existent file in existing directory) accepted or rejected. trailing pathname information can made available scripts in
path_info
environment variable.for example, assume location
/test/
points directory contains single filehere.html
. requests/test/here.html/more
,/test/nothere.html/more
both collect/more
path_info
.
all need adjust take path info part , parse it.
besides that, if want implement behavior mod_rewrite, try this:
rewritecond %{document_root}/$0.php !-f rewriterule ^(.+)/([^/]+)/([^/]+)$ /$1?$2=$3 [n,qsa] rewritecond %{document_root}/$0.php -f rewriterule .+ /$0.php [l]
Comments
Post a Comment