Title: Adding new rules through code
Last modified: March 4, 2020

---

# Adding new rules through code

 *  Resolved [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/adding-new-rules-through-code/)
 * Hi John,
 * I am the developer of Media File Renamer (among other plugins). Many of my users
   are using Redirection (and I am redirecting many of them to your plugin, in fact,:
   p), but it’s understandable that they would prefer not to have to export and 
   import the list of redirections every time.
 * Is there a function I could call to add new redirections, directly?
    Also, do
   you have a function to check if a redirection already exists, to avoid duplicates?
 * Thanks a lot 🙂

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Fanaticweb](https://wordpress.org/support/users/fanaticweb/)
 * (@fanaticweb)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12505582)
 * Looking forward to this one.
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12506120)
 * There are various ways described here, with the REST API probably being the most
   suitable.
 * [https://redirection.me/developer/](https://redirection.me/developer/)
 *  Thread Starter [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564135)
 * Thanks, [@johnny5](https://wordpress.org/support/users/johnny5/). Just to confirm;
   that doesn’t work with static files, right? I mean, with files which are in uploads
   folder for example?
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564194)
 * Redirecting a static file depends on the server setup. Some servers are configured
   to bypass PHP when serving a static file, even if that files doesn’t exist.
 * More details here: [https://redirection.me/support/problems/url-not-redirecting/](https://redirection.me/support/problems/url-not-redirecting/)
 *  Thread Starter [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564242)
 * Okay, I guess I will check this with our users 🙂 However, in my experience, 
   static files are never processed through WordPress. It would impact performances
   a lot and would skip all the specific caching related to them. The only way would
   be to actually handle those redirections in the .htaccess.
 * Thinking about that, a while ago, I created this plugin: [https://wordpress.org/plugins/media-usage/](https://wordpress.org/plugins/media-usage/).
   It forces the images to go through WordPress. That way, I was keeping stats about
   their usage and the goal was to find out the useless ones. Unfortunately, the
   impact on performances was too big.
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564252)
 * Sure, but what I meant is that some sites are (poorly) configured to try and 
   serve a static file directly even if it doesn’t exist. This results in a server
   404 page, rather than checking if the file exists and if not, allowing the 404
   to be served through WordPress.
 *  Thread Starter [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564273)
 * Indeed, that is definitely the best way to go, but in fact, how to set this up?
   I only see this possible through some config at the Apache or Nginx level, or
   maybe there is a rule we could do in the .htaccess? Sorry, maybe it’s part of
   your FAQ already, but I thought I’d ask 🙂
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564292)
 * For Apache you can use `RewriteCond` with the `-f` flag. There’s a similar rule
   for Nginx:
 * [https://serverfault.com/questions/122315/nginx-rewrite-url-only-if-file-exists](https://serverfault.com/questions/122315/nginx-rewrite-url-only-if-file-exists)
 *  Thread Starter [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564359)
 * Indeed, that’s perfect, thanks 🙂 That will be helpful for those guys wanting
   to renamed files.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Adding new rules through code’ is closed to new replies.

 * ![](https://ps.w.org/redirection/assets/icon-256x256.jpg?rev=983639)
 * [Redirection](https://wordpress.org/plugins/redirection/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/redirection/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/redirection/)
 * [Active Topics](https://wordpress.org/support/plugin/redirection/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/redirection/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/redirection/reviews/)

 * 9 replies
 * 3 participants
 * Last reply from: [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/adding-new-rules-through-code/#post-12564359)
 * Status: resolved