我有一个自定义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\');
感谢您的关注