apache - htaccess url rewrites not working as expected -
hi have been trying work out how use htaccess clean urls contain parameters , values
from www.bobbyskennel.co.uk/contact/index.php?page=site-map-page or www.bobbyskennel.co.uk/contact/?page=site-map-page
to www.bobbyskennel.co.uk/contact/site-map-page
so far have created .htaccess file , added rules below
options +followsymlinks rewriteengine on rewritecond %{script_filename} !-d rewritecond %{script_filename} !-f # contact directory sub pages rewriterule ^contact/([a-za-z0-9-]+)/?$ contact/index.php?page=$1 [l] # bobby directory sub pages rewriterule ^bobby/([a-za-z0-9-]+)/?$ bobby/index.php?page=$1 [l] # petgallery directory sub pages rewriterule ^petgallery/([a-za-z0-9-]+)/?$ petgallery/index.php?page=$1 [l]
i have placed file in root of site @ localhost , tested , rewrites work when enter clean url navigate sub page site map , cookies , privacy policy pages.
but when add slash end of url fail, yet ? character in regex pattern match expression supposed mean preceeding / @ end of match pattern optional , should work or without slash, can check on live site.
i have seen lots of examples 2 rules 1 urls no slash @ end , rule urls without slashes @ end, method seems better way of achieving same thing.
i have adjusted regex end breaking it, great if resolve issue, advice or appreciated.
i forgot explain how site structured, site divided using directories each contain index.php file , directory contains include files contain meta data , content each page within section of site.
a simple controller stored in include directory in root , included in each index.php file.
edit: apologise - looked @ site now. see problem - has nothing rewriting not working, because work.
it's fact css files referenced in relation current path ../
. need remove ..
each of these, force request relative root of site. what's happening request /contact/styles/...
(instead of /styles/...
) made when request /contact/something/
(with trailing slash). here's fix:
<link rel="stylesheet" type="text/css" media="all" href="/styles/bk-layout.css"/> <link rel="stylesheet" type="text/css" media="all" href="/styles/bk-pages.css"/>
that said, still recommend looking @ original answer below - it'll save time, , keep site seo-friendly.
give following try:
options +followsymlinks rewriteengine on # skip direct requests existing directories , files rewritecond %{request_filename} -d rewritecond %{request_filename} -f rewriterule ^ - [l] # trim trailing slash when not requesting directory # (we not use condition here # covered in first part above) rewriterule ^(.+)/$ /$1 [r=302,l] # now, check /{dir}/{slug} and, if {dir} exists, # rewrite request /{dir}/index.php?page={slug} rewritecond %{request_uri} ^/([a-z0-9-]+)/([a-z0-9-]+)$ rewritecond %{document_root}/%1/ -d rewriterule ^ /%1/index.php?page=%2 [l]
the purpose of alternative file trim out trailing slashes , not allow them when rewriting (this prevents duplicate content, results in better seo) , make rewriting process easier.
so, process this:
- the first part skips direct requests existing directories , files. if such request made, nothing happens point, except directory or file being requested served.
- the second part trims out trailing slash if request not existing directory, i.e.
/contact/
. don't need use condition here first part covers directory-check. - this fun part. request made
/contact/hello
. first condition checks request uri, , passes on first segment second condition, checks see if exists. in case, check see if/contact/
exists. if so, rewriteindex.php
file stored in directory.
the third point above alternative method think considerably won't need add new rules each directory , index file created.
the second point above answer question. generally, when having problems trailing slashes when made optional, solution opt non-slash route, , trimming them out too, google doesn't see duplicate content.
note temporary redirect: i've left trailing slash redirect 302. should decide prefer behaviour, change 301 make cachable , permanent.
Comments
Post a Comment