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
You can redirect your
non-php
pages tophp
using RewriteRule directive in your WordPress htaccess.At the top of your htaccess file , put the following rule :
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.
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.