admin管理员组

文章数量:1026373

I'm creating som custom templates in Wordpress and I'm passing some data in the URL's.

Currently my URL looks like this: /?id=43&name=designer+name

The URL contains designer ID and designer name.

I would really like to use this: +name/

My permalink structure is set to /%category%/%postname%/

My .htaccess file looks like this:

# 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

I've looked at , but not become any wiser.

I got two questions:

  1. Is there any point in changing the above URL regarding SEO
  2. How can I get the "pretty" URL?

UPDATE
I'm adding some more info for clarification.

  • I'm developing the site myself
  • The template is 100% custom template
  • Based on info from $_GET, I load data from my custom DB table and display this on page
  • My site has an avarage of between 3-400 unique visitors per day. At peak I have 2000 unique visitors per day.
  • I'm developing an online fashion magazine
  • My URL is currently being created like this:

    Url = get_permalink().'?bid='.$brand->id.'&name='.$brand->name;

  • I've used similar method here:
    /?brandID=4673&storeID=0&brand=Moods+of+Norway

Using Custom Post Type was an option I considered, but I needed my own table structurem because designers / brands are linked up against galleries (and more links to other tables to come). So saving brands data in WP POST table was not going to work.

So I need to know this:

  • What do I need to do with my Permalink structure?
  • What do I need to do with my .htaccess file?

I'm creating som custom templates in Wordpress and I'm passing some data in the URL's.

Currently my URL looks like this: http://www.mysite/designers/?id=43&name=designer+name

The URL contains designer ID and designer name.

I would really like to use this: http://www.mysite/designers/designer+name/

My permalink structure is set to /%category%/%postname%/

My .htaccess file looks like this:

# 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

I've looked at http://codex.wordpress/Using_Permalinks, but not become any wiser.

I got two questions:

  1. Is there any point in changing the above URL regarding SEO
  2. How can I get the "pretty" URL?

UPDATE
I'm adding some more info for clarification.

  • I'm developing the site myself
  • The template is 100% custom template
  • Based on info from $_GET, I load data from my custom DB table and display this on page
  • My site has an avarage of between 3-400 unique visitors per day. At peak I have 2000 unique visitors per day.
  • I'm developing an online fashion magazine
  • My URL is currently being created like this:

    Url = get_permalink().'?bid='.$brand->id.'&name='.$brand->name;

  • I've used similar method here:
    http://storelocator.no/search/?brandID=4673&storeID=0&brand=Moods+of+Norway

Using Custom Post Type was an option I considered, but I needed my own table structurem because designers / brands are linked up against galleries (and more links to other tables to come). So saving brands data in WP POST table was not going to work.

So I need to know this:

  • What do I need to do with my Permalink structure?
  • What do I need to do with my .htaccess file?
Share Improve this question edited Jan 21, 2011 at 18:27 hakre 12.9k6 gold badges49 silver badges85 bronze badges asked Nov 2, 2010 at 0:11 StevenSteven 2,6207 gold badges41 silver badges61 bronze badges 8
  • @Steven: Is this a custom post type with a rewrite setting? (Please add the register_post_type code if it is.) If you create a regular blog post, what is its address? – Jan Fabry Commented Nov 2, 2010 at 8:54
  • @Steve - Ah, I see you did ask over here. Great! – MikeSchinkel Commented Nov 2, 2010 at 9:03
  • This is not a custom post type. It's just a template that retrieves data from a DB based on info passed by $_GET(). – Steven Commented Nov 2, 2010 at 9:22
  • @Steven: OK, so you want to know how to get URL's like /designer/designer_A, /designer/designer_B, ... handled by the same template, that can then access the designer name from the URL easily? This is then a question about adding your own rewrite rule? – Jan Fabry Commented Nov 2, 2010 at 10:28
  • 1 @Steve - I strongly recommend you reconsider Custom Post Types. You are trying to reinvent the wheel and a really got set of wheels already exist in WordPress. You say you can't use CPTs; will you consider maybe asking another question here on WordPress Answers that asks how to get around the roadblock you perceive with CPTs? I'll bet you'll be surprised how easy the solution will be. Create both a "Brand" CPT and a "Gallery" CPT then link them together (there are several ways to do that...) – MikeSchinkel Commented Nov 3, 2010 at 3:10
 |  Show 3 more comments

3 Answers 3

Reset to default 4

I don't know (or didn't know) much about the rewrite rules myself (but it seems nobody does), but based on some other answers here, I got this to work. We add a new rewrite rule that matches designers/designer_name/. We "flush" the rewrite rules so they get saved to the database, but make sure to do this only once, since it is an expensive operation. Now, we set up a pattern that will match our page and save the extra part in the designer_name query variable. Since WordPress does not know it must look at this variable, we hook into the query_vars filter and tell it to look at that too.

Now, in the page-designers.php theme file, we can do get_query_var('designer_name') and it will give you the designer name. If you want extra stuff like paging (designer/designer_name/page/2), you need to add an extra rewrite rule for that (or feeds, or whatever that starts with designer/designer_name). But the basic stuff should work.

<?php
/*
Plugin Name: WPA 3537
Plugin URI: http://wordpress.stackexchange/questions/3537/need-help-with-friendly-urls-in-wordpress
Description: Need help with friendly URL's in Wordpress
Version: 1.0
Author: Jan Fabry
*/

register_activation_hook(__FILE__, 'wpa3537_flush_rules');
function wpa3537_flush_rules()
{
    add_rewrite_rule('designers/([^/]+)', 'index.php?pagename=designers&designer_name=$matches[1]', 'top');
    flush_rewrite_rules(false);
}

add_filter('query_vars', 'wpa3537_query_vars');
function wpa3537_query_vars($query_vars)
{
    $query_vars[] = 'designer_name';
    return $query_vars;
}

First I have a question: How are you getting URLs that look like the following?

http://www.example/designers/?id=43&name=designer+name

Those are not standard URLs for WordPress; is that the way you envision is should work, or does your site actually work that way? And if so, how did those URLs come to be? Is it a highly customized site? Who customized it; you, or someone else?

About .htaccess

That said, we'll start with .htaccess to get that out of the way. Yours is identical to practically every other WordPress installation's .htaccess file except for installs that 1.) serve off a URL subdirectory (and then the difference is trivial), 2.) have had people who don't understand how WordPress routes URLs mucking around them and/or 3.) except for those rare advanced installs that actually do need .htaccess mucking (and if you don't know why you would need to do it chance are almost 100% that you don't.)

About Leading Categories in WordPress URLs

Next, your desired URL format with category name in the first path segment is pattern that the WordPress community frowns upon for performance reasons, see this:

  • Category in Permalinks Considered Harmful

Basically the problem is if you have more than a handful for "Pages" (i.e. $post->post_type=='page') then WordPress will compare every URL for every page load against the URL for every Page and that can slow down your system. That said, if you only have a handful of Pages and you don't have a high-traffic site, it's my opinion not to sweat it.


(source: mikeschinkel)

Now on to your questions.

1.) Is there any point in changing the above URL regarding SEO

Yes. Cleaning up the URL improves your keyword densities. That said, in my opinion there's a lot more to SEO than just what the search engine sees and making your URLs friendlier will give people more confidence in them and make it more likely they will link to your site in blogs and share your links on Twitter, in Facebook and elsewhere. That creates two benefits; more links the search engines see and more exposure from the sharing.

Personally I like to think of it as how one dresses; if you are really messy then people are going to have a lower subconscious opinion of you than if you are meticulous. I think the same is true with URLs; if you have clean, user friendly URLs your house appears to be in order; if you have sloppy URLs your site will just not "feel" the same, again IMO. (Nasty URLs are one reason I loathe using LinkedIn. I think they'd have been more successful had they paid attention to the URLs like Twitter did, for example.)

2.) How can I get the "pretty" URL?

I think this article and/or this page on Codex should be able to help?

  • Optimizing WordPress Permalinks

  • Settings Permalinks SubPanel (on Codex)

Basically you need to set your Permalinks template and then save them. You might have to use FTP to change the permission settings on .htaccess before WordPress can save them for you. Hopefully this screenshot will be enough to illustrate what you need to do:


(source: mikeschinkel)

Let me know if this answers your question and if not where you are stuck.

Other Permalink Answers

In addition there are lots of other answers to questions about Permalinks in WordPress here at WordPress Answers; some of these might be of help to you too, in no particular order:

  • Setting up WordPress with Custom Permalinks and no .htaccess File?

  • WordPress overrides the GET variables, (page_id) set in HTACCESS?

  • Minimal custom permalink structure?

  • Custom Post Type Rewrite Rule for Author & Paging?

  • Creating 301 Redirects for Post, Page, Category and Image URLs?

  • Force the Website URL to Include “www” and to be Upper Case?

I did all Jan Fabry wrote, but when I call:

http://www.mysite/customPostTypeSlug/page/id

Wordpress redirect (301) me to:

http://www.mysite/customPostTypeSlug/page

My rule is (called by an "init" action):

add_rewrite_rule(
        'customPostTypeSlug/page/([^/]+)/?' ,
        'index.php?customPostTypeSlug=page&id=$matches[1]',
        'top'
    );

Then I flush it.

I change permalink from %category%/%postname% to %postname% and seems to work.

I'm creating som custom templates in Wordpress and I'm passing some data in the URL's.

Currently my URL looks like this: /?id=43&name=designer+name

The URL contains designer ID and designer name.

I would really like to use this: +name/

My permalink structure is set to /%category%/%postname%/

My .htaccess file looks like this:

# 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

I've looked at , but not become any wiser.

I got two questions:

  1. Is there any point in changing the above URL regarding SEO
  2. How can I get the "pretty" URL?

UPDATE
I'm adding some more info for clarification.

  • I'm developing the site myself
  • The template is 100% custom template
  • Based on info from $_GET, I load data from my custom DB table and display this on page
  • My site has an avarage of between 3-400 unique visitors per day. At peak I have 2000 unique visitors per day.
  • I'm developing an online fashion magazine
  • My URL is currently being created like this:

    Url = get_permalink().'?bid='.$brand->id.'&name='.$brand->name;

  • I've used similar method here:
    /?brandID=4673&storeID=0&brand=Moods+of+Norway

Using Custom Post Type was an option I considered, but I needed my own table structurem because designers / brands are linked up against galleries (and more links to other tables to come). So saving brands data in WP POST table was not going to work.

So I need to know this:

  • What do I need to do with my Permalink structure?
  • What do I need to do with my .htaccess file?

I'm creating som custom templates in Wordpress and I'm passing some data in the URL's.

Currently my URL looks like this: http://www.mysite/designers/?id=43&name=designer+name

The URL contains designer ID and designer name.

I would really like to use this: http://www.mysite/designers/designer+name/

My permalink structure is set to /%category%/%postname%/

My .htaccess file looks like this:

# 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

I've looked at http://codex.wordpress/Using_Permalinks, but not become any wiser.

I got two questions:

  1. Is there any point in changing the above URL regarding SEO
  2. How can I get the "pretty" URL?

UPDATE
I'm adding some more info for clarification.

  • I'm developing the site myself
  • The template is 100% custom template
  • Based on info from $_GET, I load data from my custom DB table and display this on page
  • My site has an avarage of between 3-400 unique visitors per day. At peak I have 2000 unique visitors per day.
  • I'm developing an online fashion magazine
  • My URL is currently being created like this:

    Url = get_permalink().'?bid='.$brand->id.'&name='.$brand->name;

  • I've used similar method here:
    http://storelocator.no/search/?brandID=4673&storeID=0&brand=Moods+of+Norway

Using Custom Post Type was an option I considered, but I needed my own table structurem because designers / brands are linked up against galleries (and more links to other tables to come). So saving brands data in WP POST table was not going to work.

So I need to know this:

  • What do I need to do with my Permalink structure?
  • What do I need to do with my .htaccess file?
Share Improve this question edited Jan 21, 2011 at 18:27 hakre 12.9k6 gold badges49 silver badges85 bronze badges asked Nov 2, 2010 at 0:11 StevenSteven 2,6207 gold badges41 silver badges61 bronze badges 8
  • @Steven: Is this a custom post type with a rewrite setting? (Please add the register_post_type code if it is.) If you create a regular blog post, what is its address? – Jan Fabry Commented Nov 2, 2010 at 8:54
  • @Steve - Ah, I see you did ask over here. Great! – MikeSchinkel Commented Nov 2, 2010 at 9:03
  • This is not a custom post type. It's just a template that retrieves data from a DB based on info passed by $_GET(). – Steven Commented Nov 2, 2010 at 9:22
  • @Steven: OK, so you want to know how to get URL's like /designer/designer_A, /designer/designer_B, ... handled by the same template, that can then access the designer name from the URL easily? This is then a question about adding your own rewrite rule? – Jan Fabry Commented Nov 2, 2010 at 10:28
  • 1 @Steve - I strongly recommend you reconsider Custom Post Types. You are trying to reinvent the wheel and a really got set of wheels already exist in WordPress. You say you can't use CPTs; will you consider maybe asking another question here on WordPress Answers that asks how to get around the roadblock you perceive with CPTs? I'll bet you'll be surprised how easy the solution will be. Create both a "Brand" CPT and a "Gallery" CPT then link them together (there are several ways to do that...) – MikeSchinkel Commented Nov 3, 2010 at 3:10
 |  Show 3 more comments

3 Answers 3

Reset to default 4

I don't know (or didn't know) much about the rewrite rules myself (but it seems nobody does), but based on some other answers here, I got this to work. We add a new rewrite rule that matches designers/designer_name/. We "flush" the rewrite rules so they get saved to the database, but make sure to do this only once, since it is an expensive operation. Now, we set up a pattern that will match our page and save the extra part in the designer_name query variable. Since WordPress does not know it must look at this variable, we hook into the query_vars filter and tell it to look at that too.

Now, in the page-designers.php theme file, we can do get_query_var('designer_name') and it will give you the designer name. If you want extra stuff like paging (designer/designer_name/page/2), you need to add an extra rewrite rule for that (or feeds, or whatever that starts with designer/designer_name). But the basic stuff should work.

<?php
/*
Plugin Name: WPA 3537
Plugin URI: http://wordpress.stackexchange/questions/3537/need-help-with-friendly-urls-in-wordpress
Description: Need help with friendly URL's in Wordpress
Version: 1.0
Author: Jan Fabry
*/

register_activation_hook(__FILE__, 'wpa3537_flush_rules');
function wpa3537_flush_rules()
{
    add_rewrite_rule('designers/([^/]+)', 'index.php?pagename=designers&designer_name=$matches[1]', 'top');
    flush_rewrite_rules(false);
}

add_filter('query_vars', 'wpa3537_query_vars');
function wpa3537_query_vars($query_vars)
{
    $query_vars[] = 'designer_name';
    return $query_vars;
}

First I have a question: How are you getting URLs that look like the following?

http://www.example/designers/?id=43&name=designer+name

Those are not standard URLs for WordPress; is that the way you envision is should work, or does your site actually work that way? And if so, how did those URLs come to be? Is it a highly customized site? Who customized it; you, or someone else?

About .htaccess

That said, we'll start with .htaccess to get that out of the way. Yours is identical to practically every other WordPress installation's .htaccess file except for installs that 1.) serve off a URL subdirectory (and then the difference is trivial), 2.) have had people who don't understand how WordPress routes URLs mucking around them and/or 3.) except for those rare advanced installs that actually do need .htaccess mucking (and if you don't know why you would need to do it chance are almost 100% that you don't.)

About Leading Categories in WordPress URLs

Next, your desired URL format with category name in the first path segment is pattern that the WordPress community frowns upon for performance reasons, see this:

  • Category in Permalinks Considered Harmful

Basically the problem is if you have more than a handful for "Pages" (i.e. $post->post_type=='page') then WordPress will compare every URL for every page load against the URL for every Page and that can slow down your system. That said, if you only have a handful of Pages and you don't have a high-traffic site, it's my opinion not to sweat it.


(source: mikeschinkel)

Now on to your questions.

1.) Is there any point in changing the above URL regarding SEO

Yes. Cleaning up the URL improves your keyword densities. That said, in my opinion there's a lot more to SEO than just what the search engine sees and making your URLs friendlier will give people more confidence in them and make it more likely they will link to your site in blogs and share your links on Twitter, in Facebook and elsewhere. That creates two benefits; more links the search engines see and more exposure from the sharing.

Personally I like to think of it as how one dresses; if you are really messy then people are going to have a lower subconscious opinion of you than if you are meticulous. I think the same is true with URLs; if you have clean, user friendly URLs your house appears to be in order; if you have sloppy URLs your site will just not "feel" the same, again IMO. (Nasty URLs are one reason I loathe using LinkedIn. I think they'd have been more successful had they paid attention to the URLs like Twitter did, for example.)

2.) How can I get the "pretty" URL?

I think this article and/or this page on Codex should be able to help?

  • Optimizing WordPress Permalinks

  • Settings Permalinks SubPanel (on Codex)

Basically you need to set your Permalinks template and then save them. You might have to use FTP to change the permission settings on .htaccess before WordPress can save them for you. Hopefully this screenshot will be enough to illustrate what you need to do:


(source: mikeschinkel)

Let me know if this answers your question and if not where you are stuck.

Other Permalink Answers

In addition there are lots of other answers to questions about Permalinks in WordPress here at WordPress Answers; some of these might be of help to you too, in no particular order:

  • Setting up WordPress with Custom Permalinks and no .htaccess File?

  • WordPress overrides the GET variables, (page_id) set in HTACCESS?

  • Minimal custom permalink structure?

  • Custom Post Type Rewrite Rule for Author & Paging?

  • Creating 301 Redirects for Post, Page, Category and Image URLs?

  • Force the Website URL to Include “www” and to be Upper Case?

I did all Jan Fabry wrote, but when I call:

http://www.mysite/customPostTypeSlug/page/id

Wordpress redirect (301) me to:

http://www.mysite/customPostTypeSlug/page

My rule is (called by an "init" action):

add_rewrite_rule(
        'customPostTypeSlug/page/([^/]+)/?' ,
        'index.php?customPostTypeSlug=page&id=$matches[1]',
        'top'
    );

Then I flush it.

I change permalink from %category%/%postname% to %postname% and seems to work.

本文标签: customizationNeed help with friendly URL39s in Wordpress