Solved

How to create custom SEO addresses in WordPress 4.0?

Posted on 2014-10-05
9
219 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
ID: 40362980
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
ID: 40363862
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
ID: 40364148
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40364160
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40364307
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
ID: 40364332
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
ID: 40364459
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
ID: 40364478
Thanks Jason,
And yes, this is for SEO purposes.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40364491
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

789 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