Solved

How to create custom SEO addresses in WordPress 4.0?

Posted on 2014-10-05
9
213 Views
Last Modified: 2014-10-06
I am running WordPress 4.0 on a Windows Server that has PHP enabled. The blog works fine. Although upon installation, and with the Permalinks Setting set to "post name", the address of any particular page on my site always begins with index.php - such as:

http://mydomain.com/index.php/book-detail/

I'm not concerned about that, although it should also be noted that WordPress did not create an .htaccess file but perhaps that's not significant either.

My problem: I would like to set up an .htaccess file so that any link such as:
http://mydomain.com/index.php/book-detail/?booktitle=my-book-title&isbn=12345

...would go to the book detail page and the full address would resolve to:
http://mydomain.com/index.php/book-detail/12345/my-book-title/

I tried using various examples I found by Googling this issues, but they didn't seem to kick in at all - I think the examples I found were not WordPress specific. Can anyone advise on how this could be accomplished in WordPress 4.0?

thanks experts!
0
Comment
Question by:rascal
  • 5
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
the address of any particular page on my site always begins with index.php - such as: http://mydomain.com/index.php/book-detail/ I'm not concerned about that,

Just FYI, this happens when you run WordPress on IIS.  The permalinks require mod_rewrite, which isn't present in IIS.  You can instead use the web.config file to do something similar:

http://codex.wordpress.org/Using_Permalinks#Permalinks_without_mod_rewrite

although it should also be noted that WordPress did not create an .htaccess file but perhaps that's not significant either.

It might be...depends on if you are on IIS or not.  If you are not on IIS, you may want to troubleshoot things.

I tried using various examples I found by Googling this issues, but they didn't seem to kick in at all - I think the examples I found were not WordPress specific. Can anyone advise on how this could be accomplished in WordPress 4.0?

What did you try?  Can you post samples here?
0
 
LVL 1

Author Comment

by:rascal
Comment Utility
Thanks Jason,
That explains the issue with the index.php (I am running our test version of the site on IIS, but the live version resides on a Linux server so that version does have an .htaccess file).

Here is the original .htaccess file contained on the live site:

# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window


The address of the book detail page on the live site is http://mydomain.com/book-detail/
I would like to be able to create a hyperlink that looks like:
http://mydomain.com/book-detail/?isbn=12345&booktitle=my-title and have it go to the book detail page and the address bar show the address as http://mydomain.com/book-detail/12345/my-title/

Here is what I tried on the live site:

# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^book-detail/([0-9]+)/([a-zA-Z]+)/$ /book-detail/?isbn=$1&booktitle=$2
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window


The above seems to just be ignored. It doesn't rewrite my url. I'm also concerned that since it's inside the WordPress block, it would get overwritten if the permalinks were re-saved from the WordPress admin area, but I'll deal with that once I get the correct syntax.
0
 
LVL 1

Author Comment

by:rascal
Comment Utility
I have also tried using the following within WordPress' functions.php and it also doesn't work:

add_filter( 'query_vars', 'mjf_query_vars' );
function mjf_query_vars( $query_vars )
{
    $query_vars[] = 'bookdetailisbn';
    $query_vars[] = 'bookdetailtitle';
    return $query_vars;
}
add_action( 'init', 'mjf_init_detail_page' );
function mjf_init_detail_page()
{
    add_rewrite_rule('^bookdetail/([^/]*)/([^/]*)/?','index.php?pagename=bookdetail&bookdetailisbn=$matches[1]&bookdetailtitle=$matches[2]','top');
}

Open in new window


And I tried this approach as well:

add_action( 'init', 'mjf_init_detail_page' );
function mjf_init_detail_page()
{
    add_rewrite_rule('^bookdetail/([^/]*)/([^/]*)/?','index.php?pagename=bookdetail&bookdetailisbn=$matches[1]&bookdetailtitle=$matches[2]','top');
    add_rewrite_tag('%bookdetailisbn%', '([^&]+)');
    add_rewrite_tag('%bookdetailtitle%', '([^&]+)');
}

Open in new window

0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
So the add_rewrite stuff in WordPress IS the way to do it.  However, we've found that merely adding the code is insufficient:

1) Add code to functions.php; save it.
2) Disable all permalinks (Settings | Permalinks | Default)
3) Re-enable permalinks (Settings | Permalinks | How it was before)

The disable/re-enable seems to be necessary to get the code to kick in.   Give me a few minutes and I will post a sample of how we do it.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
Comment Utility
Okay, here is our code.  We do something a little different from you, though.  In our case, we take domain.com/something/IDslug and convert it back to domain.com/something/?id=xxxxx for lookup purposes.   The code works, though, so you should be able to adapt it for your needs.

No code needed in .htaccess.

functions.php:

add_action( "init", "pleasing_permalinks" );
function pleasing_permalinks() {
	//register custom query variables
	add_rewrite_tag( '%hash%', '([^&]+)' );
	add_rewrite_tag( '%action%', '([^&]+)' );

	add_rewrite_rule(
		'^meetings/faculty/([^/]+)/?$',
		'index.php?pagename=meetings/faculty&hash=$matches[1]',
		'top'
	);
}

Open in new window

0
 
LVL 1

Author Comment

by:rascal
Comment Utility
Thanks Jason,
Although I am wondering if these just might not work on our test site because we're running IIS and that pesky index.php is always getting explicitly displayed on our URL address bar in our browser?

We did find a work-around:

The target page we're interested in is http://mydomain.com/bookdetail/

So we are just manually constructing our hyperlinks as http://mydomain.com/bookdetail/12345/title-of-book/

By doing this, when a user clicks on that link, it still takes them to the http://mydomain.com/bookdetail/ page because apparently WordPress tries to resolve as much of the URL as it can and just takes you to the last portion of the URL it can resolve, which takes you to the book detail page. And, it leaves the remaining part of the URL intact. So on the bookdetail page, we just read the 12345 (the book's ISBN) and use it to fetch and display the appropriate book.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Although I am wondering if these just might not work on our test site because we're running IIS and that pesky index.php is always getting explicitly displayed on our URL address bar in our browser?

The .htaccess solutions you tried won't work with IIS.  The internal functions.php should but you still need to clear out the old rules per my instructions.  If they don't work from that point, it's due to poor syntax.

We did find a work-around:

Or you can do that. I assume you are doing this for SEO purposes?
0
 
LVL 1

Author Closing Comment

by:rascal
Comment Utility
Thanks Jason,
And yes, this is for SEO purposes.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Just be a little careful with implementation, then.  The workaround works but WordPress will report a different link slug than what Google is expecting to see and this might (emphasis on might) trigger a spam penalty.  It shouldn't, but I would keep a close eye on GWT and Analytics.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now