Re-Write All Asset URLs in WordPress

Most of you know when you are viewing a WordPress site simply by viewing the websites HTML sourcecode in the browser. The easiest way to identify that a WordPress site is being used while looking at the sourcecode, is when you see the file paths/URLs for the sites Assets (Images, CSS, and Javascript files) and Plugins like this

In this article I will be showing you how to Re-write the URLs so that you can have a more custom look like this

As you can see, this is a much cleaner look and at first glance, it would be hard for the average Joe to be able to tell you are even using WordPress. This is not meant to try to conceal the fact that you are using WordPress, there are several other ways a user can find out that you are, this is simply to make Pretty URLs for your WordPress sites Assets!

Lets get started.

Step 1. Building our Re-write Paths

Add the following code to your WordPress Functions.php file or better yet, create a new file called something like asset_urls.php or anything that sounds good to you. Then include this new file into your Functions.php file.

Step 2. Filter Our Internal WordPress Links To Use Our New Re-writes

Now we have to make sure all the links in our WordPress site are re-written to take advantage of the re-writes we built in Step 1 above

Step 3. Update The .htaccess File

This just runs the WordPress $wp_rewrite->flush_rules() Method when we are in the WordPress Admin panel which updates our .htaccess file so that it has our new re-writes and they are always up to date

Step 4. Taking It A Step Farther

If we go back to Step 1, you can see how it is set up and you can expand this even more, you can add in support for things like fonts or AJAX back-end scripts

Final Words

  • This will not work with Child Themes or WordPress MU (Network) Installs.
  • Your server must support Re-writes (mod_rewrite) and be enabled for this to work.
  • This is not done for Security reasons but purely for Cosmetic reasons alone!
  • Most of this code is modified from the ROOTS theme framework which is amazing and does all this and more!
  • Be sure to replace THEME_NAME in the code snippets above with your actual theme name, my version does this automatically but I used file paths in this demo for demonstration purposes.

If you found this useful, then please share with your friends with the buttons below, join our Newsletter for more cool Web Developer content, or post some comments, thanks.