Wednesday 15 June 2011

apache - How to redirect all items in subfolder 1 to subfolder 2 but not subfolder itself with htaccess (permanent redirect) -



apache - How to redirect all items in subfolder 1 to subfolder 2 but not subfolder itself with htaccess (permanent redirect) -

we have rebuilt website , problem of our partner websites still link downloadable files on our server.

so illustration link might example.com/download/something.exe or example.com/download/something/abc.msi

what need if goes within folder /download/ part should substituted /download-old/ if goes example.com/download/ should leave is.

`

<ifmodule mod_rewrite.c> rewriteengine on # set "protossl" "s" if accessed via https://. used later # if enable "www." stripping or enforcement, in order ensure # don't bounce between http , https. rewriterule ^ - [e=protossl] rewritecond %{https} on rewriterule ^ - [e=protossl:s] # create sure authorization http header available php # when running cgi or fastcgi. rewriterule ^ - [e=http_authorization:%{http:authorization}] # block access "hidden" directories names begin period. # includes directories used version command systems such subversion or # git store command files. files names begin period, # command files used cvs, protected filesmatch directive # above. # # note: works when mod_rewrite loaded. without mod_rewrite, # not possible block access entire directories .htaccess, because # <directorymatch> not allowed here. # # if not have mod_rewrite installed, should remove these # directories webroot or otherwise protect them beingness # downloaded. rewriterule "(^|/)\." - [f] # if site can accessed both , without 'www.' prefix, # can utilize 1 of next settings redirect users preferred # url, either or without 'www.' prefix. take 1 option: # # redirect users access site 'www.' prefix, # (http://example.com/... redirected http://www.example.com/...) # uncomment following: # rewritecond %{http_host} . # rewritecond %{http_host} !^www\. [nc] # rewriterule ^ http%{env:protossl}://www.%{http_host}%{request_uri} [l,r=301] # # redirect users access site without 'www.' prefix, # (http://www.example.com/... redirected http://example.com/...) # uncomment following: rewritecond %{http_host} ^www\.(.+)$ [nc] rewriterule ^ http%{env:protossl}://%1%{request_uri} [l,r=301] # modify rewritebase if using drupal in subdirectory or in # virtualdocumentroot , rewrite rules not working properly. # illustration if site @ http://example.com/drupal uncomment , # modify next line: # rewritebase /drupal # # if site running in virtualdocumentroot @ http://example.com/, # uncomment next line: # rewritebase / ### boost start ### # allow alt paths set via htaccess rules; allows cached variants (future mobile support) rewriterule .* - [e=boostpath:normal] # caching anonymous users # skip boost if not request or uri has wrong dir or cookie set or request came server or https request rewritecond %{request_method} !^(get|head)$ [or] rewritecond %{request_uri} (^/(admin|cache|misc|modules|sites|system|openid|themes|node/add|comment/reply))|(/(edit|user|user/(login|password|register))$) [or] rewritecond %{https} on [or] rewritecond %{http_cookie} drupal_uid [or] rewritecond %{env:redirect_status} 200 rewriterule .* - [s=3] # gzip rewritecond %{http:accept-encoding} !gzip rewriterule .* - [s=1] rewritecond %{document_root}/cache/%{env:boostpath}/%{http_host}%{request_uri}_%{query_string}\.html\.gz -s rewriterule .* cache/%{env:boostpath}/%{http_host}%{request_uri}_%{query_string}\.html\.gz [l,t=text/html,e=no-gzip:1] # normal rewritecond %{document_root}/cache/%{env:boostpath}/%{http_host}%{request_uri}_%{query_string}\.html -s rewriterule .* cache/%{env:boostpath}/%{http_host}%{request_uri}_%{query_string}\.html [l,t=text/html] ### boost end ### # pass requests not referring straight files in filesystem # index.php. clean urls handled in drupal_environment_initialize(). rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewritecond %{request_uri} !=/favicon.ico rewriterule ^ index.php [l] rewriterule ^/download/(.+)$ /download-old/$1 [r=302,l] # rules correctly serve gzip compressed css , js files. # requires both mod_rewrite , mod_headers enabled. <ifmodule mod_headers.c> # serve gzip compressed css files if exist , client accepts gzip. rewritecond %{http:accept-encoding} gzip rewritecond %{request_filename}\.gz -s rewriterule ^(.*)\.css $1\.css\.gz [qsa] # serve gzip compressed js files if exist , client accepts gzip. rewritecond %{http:accept-encoding} gzip rewritecond %{request_filename}\.gz -s rewriterule ^(.*)\.js $1\.js\.gz [qsa] # serve right content types, , prevent mod_deflate double gzip. rewriterule \.css\.gz$ - [t=text/css,e=no-gzip:1] rewriterule \.js\.gz$ - [t=text/javascript,e=no-gzip:1] <filesmatch "(\.js\.gz|\.css\.gz)$"> # serve right encoding type. header set content-encoding gzip # forcefulness proxies cache gzipped & non-gzipped css/js files separately. header append vary accept-encoding </filesmatch> </ifmodule> </ifmodule>

`

for redirection, have 2 options:

you can utilize simple redirectmatch apache directive

redirectmatch ^/download/(.+)$ http://%{http_host}/download-old/$1

note: line can placed , outside line <ifmodule mod_rewrite.c>. makes utilize of mod_alias, create sure enabled.

you can utilize rewriterule

rewriterule ^download/(.+)$ http://%{http_host}/download-old/$1 [l,r=301]

note: can placed before ### boost start ### line

apache .htaccess redirect subfolder

No comments:

Post a Comment