Create permalink for my Custom URL

In WordPress, I am trying to redirect all requests with “somestring” in the url to a specific template

That means: http://mysite.local/mysitename/somestring to a specific template in file somefile.php below is my code:

Read More
function custom_templates(){

  /* To load template for specific page URLS */
    if(strpos($_SERVER['REQUEST_URI'], 'somestring')){    
  if(substr($_SERVER['REQUEST_URI'], -1) != '/' || substr($_SERVER['REQUEST_URI'], -2) == '//')   {
    $auth_vars = end(array_filter(explode("/", $_SERVER['REQUEST_URI'])));
    $url="/somestring/".$auth_vars."/";
   wp_redirect($url , 301);  
   exit;
  }
    // if we have a 404 status
  if ($wp_query->is_404) {
      // set status of 404 to false
      $wp_query->is_404 = false;
      $wp_query->is_archive = true;
  }
  header("HTTP/1.1 200 OK");
    include (get_stylesheet_directory() . '/somefile.php');
    exit;
    }
}

add_action('template_redirect', 'custom_templates');

it’s working in the following cases:

  1. http://mysite.local/mysitename/somestring?dummyquerystring
  2. http://mysite.local/mysitename/somestring/////
  3. http://mysite.local/mysitename/somestring?dummyquerystring/////

Not working in following cases:

  1. http://mysite.local/mysitename/somestring/?dummyquerystring
  2. http://mysite.local/mysitename/somestring/?dummyquerystring/
  3. http://mysite.local/mysitename/somestring/?dummyquerystring////

How can I make it work exactly in the same way WordPress handles URLS

Here’s my .htaccess code

RewriteBase /mysitename
RewriteRule ^index.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(.*.php)$ $1 [L]
RewriteRule . index.php [L]

Related posts

Leave a Reply