我有一个自定义404页,但不想为每一个可能出现的损坏资产提供它。相反,我只想为Apache 404提供更快的服务。我发现以下htaccess规则将实现Im所追求的目标:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \\.(jpg|jpeg|png|gif|ico|swf|bmp|js|css)$ - [nocase,redirect=404,last]
而是在之前手动将其放置在htaccess文件中
# BEGIN WordPress
我想通过主题以编程方式添加规则,因为我也试图了解WP的重写API。我当前的规则如下,其中手动添加了关键行以进行测试:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteRule ^assets/css/(.*) /wp-content/themes/tr_roots_0.4.1/assets/css/$1 [QSA,L]
RewriteRule ^assets/js/(.*) /wp-content/themes/tr_roots_0.4.1/assets/js/$1 [QSA,L]
RewriteRule ^assets/img/(.*) /wp-content/themes/tr_roots_0.4.1/assets/img/$1 [QSA,L]
RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \\.(jpg|jpeg|png|gif|ico|swf|bmp|js|css)$ - [nocase,redirect=404,last]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
我一直在寻找
examples 在和中
effort 尝试
achieve 这一点,但它们似乎过于复杂,无法直接适用。有人能试着把我引向正确的方向吗?到目前为止,我已经做到了(没有做到这一点):
function tr_flush_rewrites() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function tr_add_rewrites() {
global $wp_rewrite;
$tr_new_non_wp_rules = array(
\'\\.(jpg|jpeg|png|gif|ico|swf|bmp)$\' => \'nocase,redirect=404,last\',
);
$wp_rewrite->non_wp_rules = $tr_new_non_wp_rules + $wp_rewrite->non_wp_rules;
}
add_action(\'generate_rewrite_rules\', \'tr_add_rewrites\');
add_action(\'admin_init\', \'tr_flush_rewrites\');
感谢您的关注
最合适的回答,由SO网友:orionrush 整理而成
不幸的是,这一非常有用的页面被隐藏在codex:
基本上,mod\\u rewrite\\u rules hook可以完成这个任务。
function faster_htaccess_rules( $rules ){
$faster_rules = <<<EOD
\\n # BEGIN faster Apache 404 rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \\.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [R=404,L]
#Use instead for network sites
#RewriteRule \\.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [nocase,redirect=404,last]
# END faster Apache 404 rules\\n\\n
EOD;
return $faster_rules . $rules;
}
add_filter(\'mod_rewrite_rules\', \'faster_htaccess_rules\');
此预结束/A在
# Begin WordPress
块,但在外部
<IfModule mod_rewrite.c>
. 我一直在探索
add_rewrite_rule()
函数,但它不允许您修改重写规则标志。