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
https://forums.phpfreaks.com/topic/281552-rewrite-url/
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
https://forums.phpfreaks.com/topic/281552-rewrite-url/#findComment-1446778
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.