Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to understand how can create custom rewrites for pages/categories. Ex i have example.com/?page_id=178 and for this page have a contact form and i want this: %slug%.php

example.com/contact.php

In the permalinks i have two choices. 1) Is the Post name and the 2) is the Custom Structure : %postname%/ . With both cases i didn't have the result that i want. It is possible to do that?

share|improve this question
add comment

2 Answers 2

If you want to have domain.tld/PAGE-NAME.php then you could just use %postname%.php as custom structure.

// Edit

As this approach seems to work for posts only, you might have a try with the structure /%postname%/. In addition, you put the following lines to your .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).php /$1/ [R=301,L]
</IfModule>

// Edit, again

Replacing in the plugin file of .html on PAGES each occurrence of "html" with "php" AND having set permalinks to /%postname%.php should do the trick. I have not checked the plugin for deprecated functions and the like - it works, though.

share|improve this answer
    
Dam, in the live site i receive a 404 - Not Found with or without /%postname% or /%postname%.php. When i am in the localhost works only the /%postname% , for the case of /%postname%/.php don't work. –  MikeD Mar 5 '13 at 0:14
    
I just tested my suggestion, and you're right. This works for POSTS only (not for pages). You could use the structure /%postname%/ and add a RewriteRule to your .htaccess (see my edit above). –  t f Mar 5 '13 at 0:27
    
It is not possible to do that. My host provider didn't support .htaccess –  MikeD Mar 5 '13 at 0:42
    
I just found this (old) plugin: .html on PAGES and tested it. In the php file, you just have to replace each occurrence of 'html' with 'php', AND set your permalinks to /%postname%.php. This should do the trick. –  t f Mar 5 '13 at 1:13
    
Thanks for the share. This plugin Requires: 2.5.1 or higher, and i have the 3.4. If i have installed and then uninstall It will affect the permalinks? I don't want to use with posts. Only for pages/categories. –  MikeD Mar 5 '13 at 1:45
show 10 more comments

Here is what you want to have in the plugin file:

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


function php_page_permalink() {
    global $wp_rewrite;
    $wp_rewrite->get_page_permastruct();
    $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;
    $wp_rewrite->get_page_permastruct();
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.php';
    $wp_rewrite->flush_rules();
}

function deactive() {
    global $wp_rewrite;
    $wp_rewrite->get_page_permastruct();
    $wp_rewrite->page_structure = str_replace(".php","",$wp_rewrite->page_structure);
    $wp_rewrite->flush_rules();
}

// Edit

I forgot to mention that with this code, you can leave your permalink settings the way you want (e.g. like /%postname%/ if you want your posts to be formatted that way).

share|improve this answer
    
And? Is it working for you as well? If not, what's the problem? What is working and what is not? –  t f Mar 5 '13 at 10:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.