Solved

How to create custom SEO addresses in WordPress 4.0?

Posted on 2014-10-05
9
215 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
This video teaches users how to migrate an existing Wordpress website to a new domain.

932 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

12 Experts available now in Live!

Get 1:1 Help Now