Mod_Rewrite quetion for Wordpress

Status
Not open for further replies.

Unarmed Gunman

Medium Pimpin'
May 2, 2007
7,335
287
0
The D
www.googlehammer.com
I thought this would be a fairly simple thing to do, but after hours of tinkering and searching, I can't make it happen. I need to simply append the .php to the end of a number of pages on a WP site, By default, it doesn't append any extension. At first I tried to do it from the permalinks settings, then I tried to do it with the .htaccess file using mod-rewrite, but I'm not getting the rule right, namely because the pages are in directories, ie:

I need to turn

mysite.com/directory1/subdirectory/page

into

mysite.com/directory1/subdirectory/page.php

Because all of the old links and pagerank etc are pointed at the .php. Any help would be greatly appreciated.
 


No luck - that still produces a "pag enot found" when I go to the url with the .php appended. Here is the full .htaccess I'm using:

Code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^subdirectory/page?$ /subdirectory/page.php [PT]
RewriteRule ^subdirectory/page.php/ [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
 
Do I need to change RewriteBase / to something like RewriteBase /directory/subdomain/ since the pages I'm trying to affect are a few levels deep? Also, the file names do indeed end in numbers ie item_12 needs to be redirected to item_12.php so is there a special way to handle that?
 
I thought this would be a fairly simple thing to do, but after hours of tinkering and searching, I can't make it happen. I need to simply append the .php to the end of a number of pages on a WP site, By default, it doesn't append any extension. At first I tried to do it from the permalinks settings, then I tried to do it with the .htaccess file using mod-rewrite, but I'm not getting the rule right, namely because the pages are in directories, ie:

I need to turn

mysite.com/directory1/subdirectory/page

into

mysite.com/directory1/subdirectory/page.php

Because all of the old links and pagerank etc are pointed at the .php. Any help would be greatly appreciated.

Three things to try:

1. I would use 301 redirects instead. I don't have my .htaccess quick reference handy, but that would probably work. I've read this maintains SEO rankiongs.

2. Set up the moved original posts using page.php in the permalinks.

3. Set up permalinks to %postname%.php (check if I got the exact syntax correct)
 
I need to simply append the .php to the end of a number of pages on a WP site, By default, it doesn't append any extension.

Because all of the old links and pagerank etc are pointed at the .php. Any help would be greatly appreciated.

If you can't change the internal links to .php the next best thing is to 301 the external links to match your internal (non-.php) ones.

RewriteRule ^(directory1/subdirectory/page)$ $1.php [NC,R=301]

Also, maybe this part has some legit purpose but to me it looks like it leaves you vulnerable to getting index.php indexed under duplicate URLs...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It says if the requested path doesn't exist as a file or directory then serve up the index.php page, which means that page could get indexed as multiple urls, which is a bad thng. Do you know what it's there for?
 
Those were put in the .htaccess by default with the WP install. I was able to find a plug-in for wordpress that provided a workaround. The plug-in has one file, and here is the code:

Code:
<?php
/*
Plugin Name: .html on PAGES
Plugin URI: http://www.introsites.co.uk/33~html-wordpress-permalink-on-pages-plugin.html
Description: Adds .html to pages.
Author: IntroSites
Version: 1.1
Author URI: http://www.introsites.co.uk/
*/

add_action('init', 'html_page_permalink', -1);
register_activation_hook(__FILE__, 'active');
register_deactivation_hook(__FILE__, 'deactive');


function html_page_permalink() {
    global $wp_rewrite;
 if ( !strpos($wp_rewrite->get_page_permastruct(), '.php')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.php';
 }
}
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
   global $wp_rewrite;
    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
        return untrailingslashit($string);
  }else{
   return $string;
  }
}

function active() {
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.php')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.php';
 }
  $wp_rewrite->flush_rules();
}    
    function deactive() {
        global $wp_rewrite;
        $wp_rewrite->page_structure = str_replace(".php","",$wp_rewrite->page_structure);
        $wp_rewrite->flush_rules();
    }
?>

Worked like a charm.

If you can't change the internal links to .php the next best thing is to 301 the external links to match your internal (non-.php) ones.

RewriteRule ^(directory1/subdirectory/page)$ $1.php [NC,R=301]

Also, maybe this part has some legit purpose but to me it looks like it leaves you vulnerable to getting index.php indexed under duplicate URLs...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It says if the requested path doesn't exist as a file or directory then serve up the index.php page, which means that page could get indexed as multiple urls, which is a bad thng. Do you know what it's there for?
 
Hey man, I know this is late but if you change your permalink setting to:
/%category%/%postname%.php it should work, if you ever fancy switching up the way it works. Nice one on tracking down the plugin though
 
Status
Not open for further replies.