Jump to content

Rewrite URL


Jennifer2010

Recommended Posts

I'm using "Roots" theme for WordPress and it rewrites assets files like mysite.com/wp-content/themes/theme/assets/img/img.jpeg into mysite.com/assets/img/img.jpeg

 

However, I'm trying to get WordPress to rewrite the wp-content/uploads folder to just media ( mysite.com/wp-content/uploads vs mysite.com/media )

 

Can anyone tell me how to do it, provided the following rewrite file?

 

Thank you!

 

<?php
/**
 * URL rewriting
 *
 * Rewrites do not happen for multisite installations or child themes
 *
 * Rewrite:
 *   /wp-content/themes/themename/assets/css/ to /assets/css/
 *   /wp-content/themes/themename/assets/js/  to /assets/js/
 *   /wp-content/themes/themename/assets/img/ to /assets/img/
 *   /wp-content/plugins/                     to /plugins/
 *
 * If you aren't using Apache, alternate configuration settings can be found in the docs.
 *
 * @link https://github.com/retlehs/roots/blob/master/doc/rewrites.md
 */
function roots_add_rewrites($content) {
  global $wp_rewrite;
  $roots_new_non_wp_rules = array(
    'assets/(.*)'          => THEME_PATH . '/assets/$1',
    'plugins/(.*)'         => RELATIVE_PLUGIN_PATH . '/$1'
  );
  $wp_rewrite->non_wp_rules = array_merge($wp_rewrite->non_wp_rules, $roots_new_non_wp_rules);
  return $content;
}


function roots_clean_urls($content) {
  if (strpos($content, RELATIVE_PLUGIN_PATH) > 0) {
    return str_replace('/' . RELATIVE_PLUGIN_PATH,  '/plugins', $content);
  } else {
    return str_replace('/' . THEME_PATH, '', $content);
  }
}


if (!is_multisite() && !is_child_theme()) {
  if (current_theme_supports('rewrites')) {
    add_action('generate_rewrite_rules', 'roots_add_rewrites');
  }


  if (!is_admin() && current_theme_supports('rewrites')) {
    $tags = array(
      'plugins_url',
      'bloginfo',
      'stylesheet_directory_uri',
      'template_directory_uri',
      'script_loader_src',
      'style_loader_src'
    );


    add_filters($tags, 'roots_clean_urls');
  }
}

 

Link to comment
Share on other sites

Do you have Apache installed and can you access your .htaccess file?

You can perform URL rewriting from there.

 

Like this.

 

RewriteEngine On
RewriteRule ^/wp-content/uploads/(.*)$ /media/$1

 

Thank you for the response. I had already tried that before coming here and it did not work. I ended up finding out the change of directory not working was due to a folder permission error so now uploads are stored in the right location and are working. 

 

Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.