skip to Main Content

I have added .php extension to all my pages in wordpress through functions.php.

My Code –

<?php
// Do NOT include the opening PHP tag

// Add .PHP to page permalinks
add_action('init', 'ss_php_pages', -1);
function ss_php_pages() {
    global $wp_rewrite;
    
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.php')){
            $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.php';
    }
    
    $wp_rewrite->flush_rules();
}

// Remove slash from page permalinks
add_filter('user_trailingslashit', 'no_page_slash_on_ss',66,2);
function no_page_slash_on_ss($string, $type){
    global $wp_rewrite;
    
    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
        return untrailingslashit($string);
    }else{
       return $string;
      }
}

Now I want to redirect my old link (example.com/demo) to new link (example.com/demo.php)..
I have many pages there so I can’t add redirection for each manually.

Additional information:
I have added .php extension to my WordPress pages (example.com/demo + .php). Now My Old Links (example.demo or example.com/anything) which were without .php, got 404 because I have added .php to them so they have modified. Now I want to redirect my all old links like example.com/demo to example.com/demo.php or example.com/xyz to example.com/xyz.php to not to loss traffic or getting broken link.

2

Answers


  1. You can redirect your non-php pages to php using RewriteRule directive in your WordPress htaccess.

    At the top of your htaccess file , put the following rule :

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L,R=301] 
    
    Login or Signup to reply.
  2. Could you please try following htaccess Rules. Make sure htaccess is present in root folder.

    Make sure to clear your browser cache before testing your URLs.

    RewriteEngine on
    ##User is using wordpress without this rule it was adding .php to home page too which was NOT needed, hence this rule is been added.
    RewriteRule ^/?$ / [R=301,END]
    
    ##Major rules for links which are non php and should be served with php format files.
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteCond %{REQUEST_URI} !.php/?$ [NC]
    RewriteRule ^(.*)/?$ $1.php [L,R=301]
    

    For JS/CSS rewrite: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search