I am new here and have a question about URLs in WordPress. The very front-facing portion of my website uses WordPress just so I can use the CMSs and blog for my SEO. I have a real estate portion of the site written in basic PHP and I would like to see if there is a way to rewrite URLs so I can have a customer visit:

http://mysite.com/RepfirstnameReplastname

instead of

http://mysite.com/?rep=RepfirstnameReplastname

When a person now visits:

http://mysite.com/?rep=RepfirstnameReplastname

I have a header across the top float down and show the reps details. What I would like to do is just have a person go to a URL like this one instead:

http://mysite.com/RepfirstnameReplastname

I have put this code in my WordPress theme header file (header.php) and my homepage has id[10]

if(is_page(10)){
                  $url = $_SERVER['REQUEST_URI'];
                  $repurl2 = parse_url($url, PHP_URL_PATH);
                  $repurl = $_GET['rep'];

                  $config = array(
                'host'      => 'xxx',
                'username'  => 'xxxx',
                'password'  => 'xxxxx',
                'dbname'    => 'xxxxxx'
            );

            $db = new PDO('sqlsrv:Server=' . $config['host'] . ';Database=' . $config['dbname'], $config['username'], $config['password']);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $getInfo = $db->prepare("SELECT * FROM representatives WHERE Website = ?");
            $getInfo->bindValue(1, $repurl);
            $getInfo->execute();
            $rep = $getInfo->fetch(PDO::FETCH_ASSOC);
            if(isset($rep['DisplayName'])){
                $_SESSION['repage'] = $_GET['rep'];
                $_SESSION['repphoto'] = $rep['PhotographURL'];
                $_SESSION['repname'] = $rep['DisplayName'];
                $_SESSION['reptext'] = $rep['WelcomeText'];

            }

}

if(isset($_SESSION['repage'])){    
    ?>
    <div class="rep">
        <div class="container repinfo">
                <div class="row">
                <div class="span1"><img src="<?=$_SESSION['repphoto'];?>"></div>
                <div class="span11"><strong><? echo $_SESSION['repname']; ?></strong><br /><? echo $_SESSION['reptext']; ?>
                </div>
                <!--<div class="span2">
                <table>
                        <tr><td>Name:</td><td>John Doe</td></tr>
                        <tr><td>Phone:</td><td>555-555-5555</td></tr>
                        <tr><td>Email:</td><td>John@Doe.com</td></tr>
                </table>
                </div>-->
        </div><!-- row -->
        </div><!-- rep info -->
    </div><!-- rep -->
    <?
}
?>

The above grabs the info I need from an old SQL server for the rep and displays their info properly. I would just like to know how to remove ?rep= out of the URL.

  • The issue is that WordPress by default will try to query the database for a page with that name. If you aren't using the page post type, you can add a rule to override this, but then pages will stop working. A simple way around this is to use the structure /rep/RepfirstnameReplastname/ instead via a rewrite endpoint. – Milo Sep 14 '13 at 23:57
  • @Milo what would this code look like and where would i put it. In the current theme header? – user38063 Sep 15 '13 at 0:20
  • see my answer below. – Milo Sep 15 '13 at 1:18
  • @Milo Thank you so much for you help. I have just checked and I dont think this will work becuase all of our pre-printed marketing material have the url as mysite.com/RepfirstnameReplastname. Is there any other way around this? – user38063 Sep 15 '13 at 2:39
  • maybe instead of going to 'page not found' redirect it to a page that will perform the logic? – user38063 Sep 15 '13 at 2:43

Here's an endpoint solution as mentioned in my comment. First, add the endpoint by adding this code to your theme's functions.php file:

function wpa_add_rep_endpoint(){
    add_rewrite_endpoint( 'rep', EP_ROOT );
}
add_action( 'init', 'wpa_add_rep_endpoint' );

Then visit the Settings > Permalinks page in admin after adding the endpoint to flush the rewrite rules so this endpoint will take effect.

Now, within your template you'll be able to access the value of rep with get_query_var():

$requested_rep = get_query_var( 'rep' );

EDIT

Here's another option that you can use to check if a 404 is a request for one of your reps. We hook the template_redirect action which runs after the database is queried and see if is_404(). You can insert your code to query your own table for a matching rep name here. If it passes your test, send on OK status header, and load your template that displays a rep. It's a bit hacky, but it should work-

function wpa_check_rep(){
    if( is_404() ){
        global $wp_query;

        // this will contain the potential rep name
        // if the request matches the page rewrite pattern
        $request = $wp_query->query['pagename'];

        $is_valid_rep_name = false;
        // your code to check if $request is a valid rep name here
        // if it is a valid rep name, make WP send a 200 instead of 404
        // and load your desired template
        if( true === $is_valid_rep_name ){
            status_header( '200' );
            $wp_query->is_404 = null;
            include( get_template_directory() . '/rep-template.php' );
            exit();
        }
    }
}
add_action( 'template_redirect', 'wpa_check_rep' );

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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