.htaccess - Redirecting 301 non-existing files(pdf,doc) etc -
i've drupal cms based site in files regularly deleted. keep backlinks want non-existent pdf , word files redirect common page informing deletion.
is right way do? :
( picked 301 redirects on non existent image, pdf or url using .htaccess) :
rewriteengine on rewritebase / rewritecond %{request_filename} !-d # not dir rewritecond %{request_filename} !-f # not file rewriterule ^.*\.(pdf|doc|xls|xlsx|docx|zip)$ show-deleted-page.htm [r=301,l]
here drupal .htaccess file needs work:
# # apache/php/drupal settings: # # protect files , directories prying eyes. <filesmatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|entries.*|repository|root|tag|template)$"> order allow,deny </filesmatch> # don't show directory listings urls map directory. options -indexes # follow symbolic links in directory. options +followsymlinks # make drupal handle 404 errors. errordocument 404 /index.php # set default handler. directoryindex index.php index.html index.htm # override php settings cannot changed @ runtime. see # sites/default/default.settings.php , drupal_environment_initialize() in # includes/bootstrap.inc settings can changed @ runtime. # php 5, apache 1 , 2. <ifmodule mod_php5.c> php_flag magic_quotes_gpc off php_flag magic_quotes_sybase off php_flag register_globals off php_flag session.auto_start off php_value mbstring.http_input pass php_value mbstring.http_output pass php_flag mbstring.encoding_translation off </ifmodule> # requires mod_expires enabled. <ifmodule mod_expires.c> # enable expirations. expiresactive on # cache files 2 weeks after access (a). expiresdefault a1209600 <filesmatch \.php$> # not allow php scripts cached unless explicitly send cache # headers themselves. otherwise scripts have overwrite # headers set mod_expires if want caching behavior. may # fail if error occurs in bootstrap process, , may cause # problems if non-drupal php file installed in subdirectory. expiresactive off </filesmatch> </ifmodule> # various rewrite rules. <ifmodule mod_rewrite.c> rewriteengine on # block access "hidden" directories names begin period. # includes directories used version control systems such subversion or # git store control files. files names begin period, # control 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 being # downloaded. rewriterule "(^|/)\." - [f] # if site can accessed both , without 'www.' prefix, # can use 1 of following settings redirect users preferred # url, either or without 'www.' prefix. choose 1 option: # # redirect users access site 'www.' prefix, # (http://example.com/... redirected http://www.example.com/...) # uncomment following: rewritecond %{http_host} !^www\. [nc] rewriterule ^ http://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://%1%{request_uri} [l,r=301] # modify rewritebase if using drupal in subdirectory or in # virtualdocumentroot , rewrite rules not working properly. # example if site @ http://example.com/drupal uncomment , # modify following line: # rewritebase /drupal # # if site running in virtualdocumentroot @ http://example.com/, # uncomment following line: # rewritebase / # pass requests not referring directly 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] # 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 correct 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 correct encoding type. header set content-encoding gzip # force proxies cache gzipped & non-gzipped css/js files separately. header append vary accept-encoding </filesmatch> </ifmodule> </ifmodule> <filesmatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4|dll|css|js|cgi)$"> errordocument 404 default </filesmatch>
rewriteengine on rewritebase / rewritecond %{request_filename} !-d # not dir rewritecond %{request_filename} !-f # not file rewriterule ^.*\.(pdf|doc|xls|xlsx|docx|zip)$ http://example.com/show-deleted-page.htm [r=301,l]
just tested out, , works. make sure include domain, since apache attempts find "physical" file @ url.
Comments
Post a Comment