regex - .htaccess redirect all pages to homepage except few of them -
i have site 2 sub pages want keep, want redirect other sub pages homepage. current htaccess code:
# disable varnish caching on server header set set-cookie "_asomcnc=1; max-age=900; path=/;" rewriteengine on # redirect www rewritecond %{http_host} !^www\. rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,l] # redirect index.html root rewritebase / rewriterule ^index\.html$ / [nc,r,l] # remove trailing slash rewriterule ^(.*)/$ /$1 [l,r=301]
i tried few examples found here on stack overflow keep getting redirect loops, or css file , images not loaded. lets want load files these 2 folders:
- /css
- /images
and want keep 2 subpages:
- /subpage1.html
- /subpage2.html
everything else should redirected homepage.
to exclude pages being rewritten, can either use rewritecond
%{request_uri}
or short circuit rule chain.
to exit early, insert these rules @ beginning
rewriterule ^css - [l] rewriterule ^images - [l] rewriterule ^subpage1.html$ - [l] rewriterule ^subpage2.html$ - [l]
and redirect else homepage, unless home page, of course
rewritecond %{request_uri} !^/$ rewriterule ^ / [r,l]
to same rewritecond
rewritecond %{request_uri} !^/css rewritecond %{request_uri} !^/images rewritecond %{request_uri} !^/subpage1.html$ rewritecond %{request_uri} !^/subpage2.html$ rewritecond %{request_uri} !^/$ rewriterule ^ / [r,l]
this redirects home page, if request_uri
not css , not images , not subpage1.html , not subpage2.html , not home page.
when works should, may replace r
r=301
. never test r=301
.
Comments
Post a Comment