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 custom rewrite for following url...

http://www.domain.com/lp/1

and it would be parsed using

lp.php?p=1

I have tried following...

add_action( 'init', 'aagt_rewrite_add_rewrites' );
function aagt_rewrite_add_rewrites()
{
    add_rewrite_rule(
        '^lp/(\d+)/?$', // p followed by a slash, a series of one or more digits and maybe another slash
        'lp.php?p=$matches[1]',
        'top'
    );
}

but if i try url( http://www.domain.com/lp/1) after above code, it didn't work. am i missing something, i have tried flushing htaccess rules. please help, thanks.

share|improve this question
add comment

2 Answers

WordPress has two kinds of rewrite rules- internal and external. Internal rules are parsed by WordPress and routed to index.php. External rewrites get written to .htaccess and are not directed to WordPress.

Right now your rule is a mix between internal and external- you have it structured as an internal rule, but you have it incorrectly pointed to a file other than index.php.

If you want to use the WordPress environment within the code that receives this rewrite rule, it should be an internal rewrite. If you want this to be an external rule that is not processed by WordPress, then you need to change your rule to use $1 instead of $matches[1].

EDIT

here are a couple of methods, the first is the quick and dirty method -

Method One

Step 1 for both methods is to add a query var lpid to WordPress array of recognized query vars:

function wpa55759_query_vars( $query_vars ){
    $query_vars[] = 'lpid';
    return $query_vars;
}
add_filter( 'query_vars', 'wpa55759_query_vars' );

Step 2, add the rewrite rule to only set lpid when our rule is matched:

function wpa55759_rewrites(){
    add_rewrite_rule(
        'lp/(\d+)/?$',
        'index.php?lpid=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpa55759_rewrites' );  

Step 3, hook parse_request and check if our query var is set. If so, load lp.php from the template directory, and exit:

function wpa55759_parse_request( $request ){
    if( isset( $request->query_vars['lpid'] ) ):
        include( get_template_directory() . "/lp.php" );
        exit();
    endif;
    return $request;
}
add_action( 'parse_request', 'wpa55759_parse_request' );

To get the value of lpid inside lp.php:

global $wp;
echo $wp->query_vars['lpid'];

Basically we're checking for our query var and cutting out early. WordPress never runs a database query to load posts or pages. Not sure what side effects this may have otherwise.

Method Two

The second method is to create a page within WordPress, lp, and point our rule at that, also passing the query var:

function wpa55759_rewrites(){
    add_rewrite_rule(
        'lp/(\d+)/?$',
        'index.php?pagename=lp&lpid=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpa55759_rewrites' );

Now you can create a template page-lp.php and access lpid with:

echo get_query_var( 'lpid' );

One additional option with this second method is to filter template_redirect and shortcut the template hierarchy, loading your own custom file:

function wpa55759_template_redirect(){
    global $wp_query;
    if( $wp_query->get( 'lpid' ) ):
        include( get_template_directory() . "/lp.php" );
        exit();
    endif;
}
add_filter( 'template_redirect', 'wpa55759_template_redirect' );
share|improve this answer
    
and where should i have lp.php file? inside template folder or at root? Even is there any easy way to achieve this? for example if i create a new page called /lp and then is it allowed by wordpress to access domain.com/lp/var1 can you please help me with how to get var1 in template for processing with php. thanks for great help. –  seoppc Jun 19 '12 at 14:54
    
please reply, thanks –  seoppc Jun 19 '12 at 17:53
    
@seoppc - see edit –  Milo Jun 19 '12 at 22:00
add comment

go to wp-admin/options-permalink.php

nothing to do but "Save Changes"

share|improve this answer
    
it didn't help, as i have tried already. –  seoppc Jun 19 '12 at 14:00
    
'lp/(\d+)/?$', where lp.php path? –  Gembel Intelek Jun 19 '12 at 14:02
    
inside theme folder, shout it be at root? –  seoppc Jun 19 '12 at 14:15
    
ya.. it should be at root : domain.com/lp.php if you want to work inside your theme folder, you should add query var pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api –  Gembel Intelek Jun 19 '12 at 14:22
    
thanks for your help, 1 more question... would it affect wordpress permalinks if i use RewriteCond %{REQUEST_FILENAME} dj domain.com/dj i want to use these links for google analytics campaign tracking purpose. –  seoppc Jun 19 '12 at 14:27
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.