WordPress 中文文档
Using Permalinks
出自WordPress Chinese 中文文档
wordpress.org.cn
目录 |
Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. A permalink is what another weblogger will use to link to your article (or section), or how you might send a link to your story in an e-mail message. The URL to each post should be permanent, and never change — hence permalink.
Permalink Types
There are three basic types of WordPress permalinks:
Default: Ugly
The default looks like
prehttp://example.com/?p=N/pre
where varN/var is the ttPost ID/tt number. It works on all server environments, but it doesn't look as nice as some of the other options.
mod_rewrite: Pretty Permalinks
Using mod_rewrite or lighttpd you can produce much nicer permalinks (see Pretty Permalinks). There are many different formats, but the most common, and most versatile looks like
pre http://example.com/category/post-name/ or http://example.com/year/month/day/post-name/pre
Some people eliminate some or all of the date elements (day, month, year) to have a shorter permalink format.
Pretty permalinks are available under
- Apache web server with the ttmod_rewrite/tt module
- Microsoft IIS 7+ web server with the URL Rewrite 1.1+ module and PHP 5 running as FastCGI
- Microsoft IIS 6+ using ASAPI_Rewrite
- Lighttpd using a 404 handler or mod_rewrite or mod_magnet (see External sources)
PATHINFO: Almost Pretty
ttPATHINFO/tt permalinks look very much like ttmod_rewrite/tt permalinks but for one exception: they have tt/index.php/tt inserted before them, like so:
prehttp://example.com/index.php/yyyy/mm/dd/post-name//pre
Otherwise, they are the same as the pretty ttmod_rewrite/tt permalinks, and are similarly flexible. Anything that ttmod_rewrite/tt permalinks can do, ttPATHINFO/tt permalinks can do, with the help of that tt/index.php/tt part.
There is a helpful plugin that displays the type of permalinks being used and detailed information on the internal rewrite rules used by WordPress.
Choosing your permalink structure
In the Settings rarr; Permalinks panel (Options rarr; Permalinks before WordPress 2.5), you can choose one of the common structures or enter your own in the Custom structure field using the istructure tags/i.
strongPlease note:/strong You never, ever put your site url in the permalinks slot. You must use one of the structure tags, or a combination of tags only.
To activate PATHINFO permalinks, start your permalink structure with ttindex.php//tt.
File:Wordpress-permalinks-structure.gif
Structure Tags
strongPlease note:/strong
You never, ever put your site url in the permalinks slot. You must use one of the structure tags, or a combination of tags only.
You can use these tags to customize your Pretty or Almost Pretty permalinks. A few hints:
- Make sure to end your structure with either tt%post_id%/tt or tt%postname%/tt (e.g. tt/%year%/%monthnum%/%day%/%postname%//tt) so that each permalink points to an individual post.
- For performance reasons, it is not a good idea to start your permalink structure with the category, tag, author, or postname fields. The reason is that these are text fields, and using them at the beginning of your permalink structure it takes more time for WordPress to distinguish your Post URLs from Page URLs (which always use the text page slug as the URL), and to compensate, WordPress stores a lot of extra information in its database (so much that sites with lots of Pages have experienced difficulties). So, it is best to start your permalink structure with a numeric field, such as the year or post ID. See wp-testers discussion of this topic.
- %year%
- The year of the post, four digits, for example tt2004/tt
- %monthnum%
- Month of the year, for example tt05/tt
- %day%
- Day of the month, for example tt28/tt
- %hour%
- Hour of the day, for example tt15/tt
- %minute%
- Minute of the hour, for example tt43/tt
- %second%
- Second of the minute, for example tt33/tt
- %postname%
- A sanitized version of the title of the post (ipost slug/i field on Edit Post/Page panel). So #8220;This Is A Great Post!#8221; becomes ttthis-is-a-great-post/tt in the URI (see Using only tt%postname%/tt). Starting Permalinks with %postname% is strongly not recommended for performance reasons..
- %post_id%
- The unique ID # of the post, for example tt423/tt
- %category%
- A sanitized version of the category name (icategory slug/i field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI. Starting Permalinks with %category% is strongly not recommended for performance reasons.
- %tag%
- A sanitized version of the tag name (itag slug/i field on New/Edit Tag panel). Starting Permalinks with %tag% is strongly not recommended for performance reasons
- %author%
- A sanitized version of the author name. Starting Permalinks with %author% is strongly not recommended for performance reasons
Category base and Tag base
The iCategory base/i and iTag base/i are prefixes used in URLs for category and tag archives, which look like this:
example.net/wp/varcategory_base/var/varcategory_name/var example.net/wp/vartag_base/var/vartag_name/var
The default values for these are ttcategory/tt and tttag/tt. You can change them, but you can't remove them from the URLs altogether.
Custom permalinks work on most systems without any problems, but there are still some conditions where problems occur.
Using tt%category%/tt with multiple categories on a post, or tt%tag%/tt
When you assign multiple categories to a post, only one can show up in the permalink. This will be the lowest numbered category (see Manage Categories). The post will still be accessible through all the categories as normal.
The same thing applies to using the tt%tag%/tt field in your permalink structure.
Using Pretty permalinks
Requirements:
- Apache web server with the mod_rewrite module installed
- In WordPress's home directory,
- The FollowSymLinks option enabled
- ttFileInfo/tt directives allowed (e.g. codeAllowOverride FileInfo/code or codeAllowOverride All/code)
- An tt.htaccess/tt file (if this file is missing, WordPress will try to create it when you activate pretty permalinks)
- If you want WordPress to update the tt.htaccess/tt file automatically, WordPress will need write access to the file.
- For lighttpd, see External Resources.
- Mac Users running WordPress locally must edit their httpd.conf file editing the AllowOverride line to read emAllowOverride All/em in the emDirectory /Library/WebServer/Documents/em host instructions.
When you create or update a pretty permalink structure, WordPress will generate rewrite rules and attempt to insert them into the proper tt.htaccess/tt file. If it can't, it will say something like ttYou should update your .htaccess now/tt and print out the rules for you to copy and paste into the file (put them at the end).
In WordPress 2.0+ versions, you'll probably need to do this only once, because WordPress does the rewriting internally. If you ever move your WordPress home directory (iBlog address/i), you'll need to repeat this step.
WordPress will play nice with an existing tt.htaccess/tt and will not delete any existing RewriteRules or other directives. If you have other ttmod_rewrite/tt rules, put yours before WordPress's.
Where's my tt.htaccess/tt file?
Wordpress's ttindex.php/tt and tt.htaccess/tt files should be together in the directory indicated by the iBlog address (URI)/i setting on your General Options page. Since the name of the file begins with a dot, the file may not be visible through an FTP client unless you change the preferences of the FTP tool to show all files, including the hidden files. Some hosts (e.g. Godaddy) may not show or allow you to edit .htaccess if you install WordPress through the Godaddy Hosting Connection installation.
Creating and editing (tt.htaccess/tt)
If you do not already have a .htaccess file, create one. If you have shell or ssh access to the server, a simple codetouch .htaccess/code command will create the file. If you are using FTP to transfer files, create a file on your local computer, call it tt1.htaccess/tt, upload it to the root of your WordPress folder, and then rename it to tt.htaccess/tt.
You can edit the tt.htaccess/tt file by FTP, shell, or (possibly) your host's control panel.
The following permalink rewrite code should be included in your .htaccess file:
pre# BEGIN WordPress IfModule mod_rewrite.c RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] /IfModule
- END WordPress/pre
If your tt.htaccess/tt file contains errors that bring down your site (Internal Server Error (500)), you will need to use FTP or your host's control panel to delete the rogue tt.htaccess/tt file.
Automatically updating tt.htaccess/tt
If WordPress can't update your tt.htaccess/tt file automatically, it will tell you something like ttIf your .htaccess file were writable, we could do this automatically, but it isn’t…/tt near the bottom of the Settings rarr; Permalinks panel.
If you want to let WordPress do this, you'll need to give WordPress write access to the .htaccess file. The exact permissions necessary depend on your server setup. Try adding write permissions for the owner, then group, then world, testing after each change; once WordPress has edited the file successfully, don't add any further write permissions.
After applying the permalinks, you should change the permissions to something stronger like 660 or 644 to prevent others on the server from potentially having access to it.
Permalinks without ttmod_rewrite/tt
Pretty permalinks usually require ttmod_rewrite/tt, and IIS (common on Windows servers) does not support ttmod_rewrite/tt. (If you are using Apache 2.0.54, on Windows, ttmod_rewrite/tt may work, provided it is enabled in ttapache\conf\httpd.conf/tt.)
If you are using IIS 7 and have admin rights on your server, you can use Microsoft's URL Rewrite Module instead. Though not completely compatible with ttmod_rewrite/tt, it does support WordPress's pretty permalinks. Once installed, open the ttweb.config/tt file in the WordPress folder and add the following rule to the ttsystem.webServer/tt element
pre rewrite
rules
rule name=Main Rule stopProcessing=true
match url=.* /
conditions logicalGrouping=MatchAll
add input={REQUEST_FILENAME} matchType=IsFile negate=true /
add input={REQUEST_FILENAME} matchType=IsDirectory negate=true /
/conditions
action type=Rewrite url=index.php/{R:0} /
/rule
/rules
/rewrite /pre
There's a full installation guide on the IIS site. The module is available for x64 and x86 systems.
If this isn't an option, you can try PATHINFO permalinks; put ttindex.php//tt at the start of your custom permalink structure:
/index.php/%year%/%monthnum%/%day%/%postname%/
This option may not always work, especially in cases of WordPress running on IIS 6. To make this option work on IIS, add these 2 lines to a php.ini file and store that file in your webroot :
cgi.fix_pathinfo = 1 cgi.force_redirect = 0
Another solution exists using IIS' custom 404 redirects. It requires that your web host allows you to add a custom 404 redirect, but it doesn't require you to install any 3rd party mod_rewrite software and it also doesn't require that your permalink structure begin with tt/index.php//tt.
Fixing Permalink Problems
Fixing tt.htaccess/tt Generation Issues
If your installation of WordPress does not generate a .htaccess file or if it does not write the new rules onto your existing .htaccess file then there are a couple reasons that could be causing this. Work step by step and continue to the next step only if the previous step does not work.
ol liChange File Permissions: You must ttchmod/tt the tt.htaccess/tt file to 666 to edit it with the WordPress template editor, but this is not recommended, since if you do that, any user of your blog, who can edit templates will be able to edit it. You can change the permissions to 660 to make it server-writable, which again will have the same limitation./li liServer Blockage: Your host might have blocked the ttSERVER_SOFTWARE/tt variable and this will cause WordPress' .htaccess generation to fail. If you are sure that your server is running Apache, you can force WordPress to believe that your server is running Apache by changing your ttwp-includes/vars.php/tt file. Follow the steps below to implement these changes./li
ul liOpen the ttwp-includes/vars.php/tt file using the built in file editor in your WordPress Admin panel. To navigate to this panel, login to WordPress, click on Manage, then on Files, scroll to the bottom and type in ttwp-includes/vars.php/tt into the text box under the Other Files title./li Look for pre style=font-size:0.7em$is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0;/pre and replace it with pre style=font-size:0.7em// $is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0;/pre liAdd a new line under pre style=font-size:0.7em// $is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0;/pre and type in pre style=font-size:0.8em$is_apache = 1;/pre/li /ul
liUsers of XAMPP (Windows): Some versions of XAMPP do not enable ttmod_rewrite/tt by default (though it is compiled in Apache). To enable it — and thus enable WordPress to write the tt.htaccess/tt file needed to create pretty permalinks — you must open ttapache/conf/httpd.conf/tt and uncomment the line ttLoadModule rewrite_module modules/mod_rewrite.so/tt (i.e., delete the hash/pound sign at the front of the line)./li /ol
Permalinks, .htaccess, and MS Frontpage
A note about Microsoft Frontpage: many servers (shared and dedicated) maintained and built by various hosting companies come with ttmod_frontpage/tt compiled with the apache build, and in many cases with the Frontpage Server Extensions installed, on each virtual server. This is more common than not, many/most binary distributions used in the server build process at most hosting companies these days include both mod_fronpage and the server extensions. Even if you're not using Frontpage, because of the way that the extensions interact with apache (and the tthttpd.conf/tt file) you'll likely get something like a 500 error or blank white page when trying to view your WP install (although the admin panel may operate correctly) simply because ttextensions/mod_frontpage/tt exist on your server.
Wordpress will operate correctly with the Frontpage Extensions installed, however permalinks will not function at all and ANY change to the permalinks section from the Wordpress admin interface will cause corruption of the Frontpage server extensions due to the addition of the ttmod_rewrite/tt rules to the .htaccess file. There is however now a fix for this situation.
Quick Fixes, Frontpage or Permalinks
Frontpage Extensions Fix: If you don't care about permalinks and just want to make the MS Frontpage server extensions work again, simply edit your .htaccess file and remove the WordPress section with the rewrite rules.
To Use Permalinks: If you don't care about Frontpage(but your hosting company has the extensions installed)
You will need to remove (or have your hosting company do so) the MS Frontpage server extensions, or simply edit the .htaccess file to removed all of the Frontpage Lines, leaving only the WordPress mod_rewrite code.
Using FrontPage AND Permalinks Together
Finally, A solution.
There have been a number of threads on this issue in the support forums, and until now, no solution to the problem.
Normally, on a Unix server with the Microsoft FrontPage Server extensions installed WordPress works just fine and you are able to edit and publish pages (with Microsoft FrontPage) mdash; until mdash; you make a change to the permalinks (for example to the date based kind that I like /2005/04/etc). I often suggest that type of URI to folks asking about permalinks etc, as that is the method recommended by the w3c (see http://www.w3.org/Provider/Style/URI ).
Now, the problem is that FrontPage uses the .htaccess file (which the WordPress mod_rewrite rules must access) for its publishing and web authoring configuration. As soon as the WordPress mod_rewrite code is added to the file, two things happen mdash; the permalinks don't work and the Frontpage Server extensions become corrupt.
I have tried countless ways to get around this, including trying to use rewrite rules that ignore the tt%{HTTP_USERAGENT)%/tt used by FrontPage, to using a second AccessFilename tt.wpaccess/tt to the tthttpd.conf/tt file, and a host of other things, and nothing worked to allow use of FrontPage and management and use of permalinks in WordPress at the same time.
The solution is actually simple, and I figured it out by accident.
If you are using, or wish to use FrontPage (or if your hosting package is pre-configured that way) along with WordPress, you'll need to take the following simple steps on your server or have your hosting company do them for you.
Microsoft FrontPage creates the following directory pre_vti_bin/pre
Nested within that it creates both pre_vti_adm/pre and pre_vti_aut/pre
In addition to in your site (or WordPress) root folder in all of those directories you will find additional tt.htaccess/tt files.
In all three of these directories AND in the root directory, at the top of ALL of the tt.htaccess/tt files you simply need to add one line: pre Options +FollowSymlinks /pre There may or may not already be a line in each like preOptions None/pre Edit and save each tt.htaccess/tt file and you're done. Now everything works perfectly, including FrontPage, AND the permalinks of your choosing.
Long Permalinks
When using extra long permalinks in email and posting in comments and chats, some long permalinks are chopped off or only the first section is actually recognized as a link and the end seen as text. Here is an example.
div style=margin: 5px; padding:5px tt style=font-weight:bold; color:#036; text-decoration:underline; font-size:0.9emhttp://yourdomain.example.com/2005/10/4/article-about-joe-fred-sally-and-bog/tt/div
Can result in:
div style=margin:5px; padding 5pxtt span style=font-weight:bold; color:#036; text-decoration:underline; font-size:0.9emhttp://yourdomain.example.com/2005/10/4/article/span-about-joe-fred-sally-and-bog/tt /div
To click on the lower link, the user would get a 404 Page Not Found Error. If you have a tendency to use very long permalink post titles, take these steps to prevent this problem.
1. Check that you are indeed using Permalinks.
2. Edit your tt.htaccess/tt file and add the following: pre style=font-size: 0.7em
RewriteRule ^post/([0-9]+)?/?([0-9]+)?/?$ /index.php?p=$1page=$2 [QSA]
/pre
3. Test it. Find a post's ID number and type the following (with your information) in your browser and you should be redirected to your post: pre http://yourdomain.example.com/post/(the ID #) /pre
It is also worth noting that most email software will not cut off URLs that have been delineated with angle-brackets (lt; and gt;), so when pasting URLs into emails, you should write them as so:
div style=margin: 5px; padding:5px ttRead my blog post at lt;span style=text-decoration:underline; color:bluehttp://yourdomain.example.com/2005/10/4/article-about-joe-fred-sally-and-bog/spangt;/tt/div
Additionally, some decent email clients offer a preformat option when composing plain-text emails. Using the preformat option when pasting links will force the email client not to insert linebreaks inside the links.
Fixing Other Issues
If your tt.htaccess/tt file is being generated correctly, but Permalinks still do not function, the following might be a problem. If problems persist, post a note in the WordPress Forum's How To section.
- AllowOverride Not Enabled
- Your server may not have the AllowOverride directive enabled. If the AllowOverride directive is set to ttNone/tt in your Apache tthttpd.config/tt file, then tt.htaccess/tt files are completely ignored. In this case, the server will not even attempt to read tt.htaccess/tt files in the filesystem. When this directive is set to ttAll/tt, then any directive which has the tt.htaccess/tt Context is allowed in .htaccess files. Example of enabled AllowOverride directive in tthttpd.config/tt:
pre
Directory / Options FollowSymLinks AllowOverride All /Directory
/pre
You may also have to enable the AllowOverride directive in your DocumentRoot:
pre
Directory /var/www/html # ... other directives... AllowOverride All /Directory
/pre
- You may also have to change the AllowOverride settings for the site. This is surely the case when using Mac OS X Server, but might be likewise with other systems. Usually you can find the site configuration files in tt/etc/httpd/sites//tt
- If you don't want to set AllowOverride to all (as it is above) then your AllowOverride list must include the FileInfo directive. You must restart your Apache server for any tthttpd.config/tt file changes to take effect. For more information on which overrides are allowed, read about Apache Core Features.
- Paged Navigation Doesn't Work
- Sometimes navigation to second (and subsequent) pages of posts does not work as expected. Your page may generate a link to a page with one of these URIs:
pre http://www.example.com/page/2/
http://www.example.name/category/categoryname/page/2/ http://www.example/year/month/day/page/2/ http://www.example/year/month/page/2/
/pre
- The result of clicking one of those links is that the page loads with all the surroundings (header, footer, sidebar), but instead of a page of posts, there is an error message: Sorry, no posts match that criteria.
- This is due to a glitch in the tt.htaccess/tt file that WordPress generates. To fix it, delete the contents of your .htaccess file and re-create it.
ol liIn the Control Panel, go to Manage Files (More Info on Editing Files)/li liClick the link to your .htaccess file to edit its contents/li liCopy the contents of the file and paste it to a text file in a text editor. This is a precaution in case your .htaccess file has manual entries for redirects, denials or other handy htaccess tricks/li liDelete all contents from your .htaccess file and click the Update File button./li liIn the Control Panel, go to Options Permalinks./li liClick the Update Permalink Structure button to freshly generate new rewrite rules for your permalinks./li liTest the results using a link that had previously broken./li liAdd any manual htaccess entries back in your file (Place manual htaccess entries before the tt# BEGIN WordPress/tt or after tt# END WordPress/tt lines.)/li /ol
- You may also perform similar steps by deleting the tt.htaccess/tt files from the server, creating a fresh empty .htaccess file, changing its permissions to 666, and then in Options Permalinks generate a new set of htaccess rules by clicking the Update Permalinks Structure button.
- If that still doesn't work, take a look at the WordPress support forums, specifically, this support post.
- Permalinks to pages don't work
- If you've tried to navigate to a newly created Page and encounter an error, you likely need to update your Permalink structure. Remember, each time you add a new static Page to WordPress, new rules must be generated and updated to tt.htaccess/tt (WordPress 1.X) or to the internal rewrites array (WordPress 2.X).
- Permalinks to Ultimate Tag Warrior tag pages don't work
- If you get 404 errors on local tag URLs when using the UltimateTagWarrior plugin on WordPress 2.X, it's because the internal rewrites generated by WordPress are being overly greedy and getting invoked before UTW's rewrite rules have a chance. This usually occurs only when using a custom permalink structure (like tt/%postname%//tt). To fix it, either switch your Permalink structure to Date and name based or hack the UTW plugin to place the UTW rewrites at the top of the internal rewrites array.
- Permalinks work but no pages are returned
- Some versions of PHP 4.4.x and 5.x have a bug that causes mod_rewrite to fail when used with some versions of Apache 2.x. More details at http://bugs.php.net/bug.php?id=35096 and http://bugs.php.net/bug.php?id=35059.
More Help
If these steps do not work, search for your problem in the Codex, Troubleshooting, or in the Support Forum. As a last resort, file a bug report.
Tips and Tricks
If you want to list your site in Google News, one of the requirements is to have at least three digit identifier at the end of every url.
This is easily accomplished using %postname%-%post_id% structure, which will append post ID to the end of URL so it becomes http://example.com/cooking-tips-tricks-344.
Avoiding interpretation as an archive link
Note that even though one might never make more than one posting a day, and thus wishes to use e.g., %year%%monthnum%%day%, links so generated will however be interpreted as the archive of all posts for that day. One needs at least %year%%monthnum%%day%%hour% to target an individual post.
Check for permalink structure
A way to check if the blog has a permalink structure is: preif ( get_option('permalink_structure') != ) { echo 'permalinks enabled' }/pre
See Also
- Linking Posts Pages and Categories for how to link from one article to another

