使用静态链接

来自WordPress中文文档
跳转至: 导航搜索

静态链接是指向你的Blog文章,分类或其他东西的URL。其它Blogger可以使用这些链接来引用你的文章。特别的,对于单片文章而言,一旦文章发表,获得一个地址后,它就不应该再随意变动了。这也是“静态”这个词的含义。

静态链接类型

有三种静态链接类型

默认的("难看的")

默认的静态链接类似于http://example.com/?p=N,其中N是一个自然数。WordPress使用此类默认地址,是因为它适用于各种服务器环境。不过,它看上去没有另外几种好看,而且对于搜索引擎不那么友好。

mod_rewrite生成的链接 ("好看且实用")

这就是我们梦寐以求的链接形式. 他有很多种形式, 最常用的一种, 也是最实用的一种是http://example.com/年/月/日/文章_名字/ 也有人喜欢除去日期(年/月/日)的显示,还有人喜欢文章_名字.html这样的格式,这些都是允许的。 mod_rewrite 这种链接形式需要 Apache的 mod_rewrite 模块, 这就意味着没有使用Apache的服务器不能试用他.

Index.php格式 ("也很好看呢")

Index.php 这种链接形式和 mod_rewrite 格式差不多, 只有一点除外,链接中包括一段/index.php . 像这样: http://example.com/index.php/yyyy/mm/dd/post-name/ 他也可以像好看的mod_rewrite 格式那样有很多种显示格式. 只要 mod_rewrite 能做的, Index.php 格式也能做。

结构变量

对后面两种静态链接,你可以使用以下变量:

%year% 
文章发表年份,四位数字,比如2004
%monthnum% 
文章发表月份,比如05
%day% 
文章发表日期,比如 28
%hour% 
文章发表小时数,比如 15
%minute% 
文章发表分钟数,比如 43
%second% 
文章发表秒数,比如 33
%postname% 
文章标题,中文标题不推荐使用
%post_id% 
文章的id,比如423
%category% 
文章的分类
%author% 
文章作者

这些类型的变量可以在大多数的系统上正常运作,但是某些情况下仍会出现问题。

Note on using only %postname% 
If you use postname as the only element in your permalinks to create a structure such as myblog.com/post-title, the rewrite rules may make it impossible to access pages such as your stylesheet (which has a similar format) or the wp-admin folder. It's best to include some numeric data (e.g. the post ID or date) in the permalink to prevent this from happening. Additionally, WordPress v1.2.x requires the use of a date structure in order for some features, such as the calendar, to function properly. /%year%/%monthnum%/%day%/%postname%/ is always a good start.
Note on using %category% 
%category% does not work correctly with some implementations of mod_rewrite in Apache versions prior to 2. If you are using Apache 1 and experience problems with using %category, either do not use %category% in your permalink structure, or refer to Schlueterica's plugin.
Note on using %category% with multiple categories on a post 
Where you assign multiple categories to a post, only 1 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.
Properly terminating permalinks 
It's important to guarantee permalinks lead to individual posts within your custom URI, so make sure to end a virtual site structure with either %post_id% or %postname%
/%year%/%monthnum%/%day%/%postname%/

.htaccess 文件在哪里?

.htaccess file should be in the directory indicated by your "Blog address (URI)" setting on your General Options page.因为文件名的开头是由 "."开始的,所以在FTP登陆的时候,这个文件可能看不到。除非你更改FTP工具的偏好选项,来显示所有的文件,包括隐藏文件。

如果你还没有一个.htaccess 文件, 创建一个.如果你有shell工具, 或者可以通过ssh方式登陆web服务器, 你可以通过一个简单的 touch .htaccess命令将创建这样一个文件。. 如果你使用FTP,在你的本地电脑上创建一个文件,命名为 1.htaccess,把这个文件上传到你的WordPress安装目录的根目录下,并把它重命名为.htaccess. 下面阅读下面的部分来看你是否可以修改该文件。

建立重定向(Rewrite)规则 (.htaccess)

您的服务器需拥有 mod_rewrite 模块以便无冗余代码(cruft-free)的静态链接(permalink)生效。另外,您必须创建一个 .htaccess 文件并将其放置在您的 index.php 文件存在的目录下,或将该目录设为可写入状态以便 WordPress 帮您建立它。例如,假设您的 WordPress blog 被安装在 domain.com/wordpress/ 目录下,请将 .htaccess 文件放置在 domain.com/wordpress/.htaccess。不过,如果您的 WordPress 安装在一个子目录中,而您的访客通过站点的顶级域名访问它,请将 .htaccess 文件放置在 domain.com/.htaccess。(译者注:上述“domain.com”指代您的域名)

当您建立一个静态链接(permalink)结构后,WordPress 将生成重定向(Rewrite)规则并试图将它们插入正确的 .htaccess 文件。如果自动插入失败,系统将输出该规则以便您将其复制并粘贴至您的 .htaccess 文件中。

关于创建和编辑您的 .htaccess 文件的一些注意事项:

  • WordPress 能很好的操作现有的 .htaccess 文件并且不会删除您现存的规则。
  • 如果您配置有其它 mod_rewrite 规则,它们的优先级将高于 WordPress 的规则。
  • 您必须拥有 FTP 或 SSH 通路来创建 .htaccess 文件。
  • 您必须将 .htaccess 文件的访问许可设为 666 以允许 WordPress 自动将其规则写入该文件。在应用静态链接(permalink)后,您应当设置更可靠的文件访问许可,比如 660,从而防止服务器上的其他人通过潜在的通路修改它。(译者注:设置文件访问许可的操作可以通过 FTP 软件完成)
  • 如果您的 .htaccess 文件存在错误并导致站点出错,您需要使用 FTP 软件或者您主机的控制面板删除有误的 .htaccess 文件。一旦某个致命的修改在 WordPress 编辑器中被保存,该编辑器(连同您站点的其他部分)都将失效直至该问题被修复。
  • 您同样可以利用主机的控制面板创建和编辑 .htaccess 文件。因此,即使 .htaccess 文件中的错误已经导致您的站点失效,您仍旧可以在控制面板中编辑它。
  • 由于以上这些原因,建议您每次仅对 .htaccess 文件作出小改动并经常保存及测试您的站点。通过这样您就可获知何时出错,并能很快地通过 FTP 修复错误。

不用mod_rewrite模块实现静态链接

对于简洁的静态链接,你必须使用mod_rewrite模块,遗憾的是IIS(一般安装在Windows服务器上)并不支持mod_rewrite。如果你正在windows上使用Apache 2.0.54,mod_rewrite或许可以工作。如果你在开头输入一个文件名,WordPress将绕开使用mod_rewrite的必要并尝试使用它来传递参数。

/index.php/%year%/%monthnum%/%day%/%postname%/

如果你使用这种方法,你可以忽略重写规则(也就是说,你可以忽略.htaccess)。

  • 这种方法不是总是有效,特别是当WordPress运行在IIS 6上时。为了使这种方法在IIS也同样奏效,可以在你的php.ini文件添加以下两行:
cgi.fix_pathinfo = 1
cgi.force_redirect = 0

原文出自Cem via http://blog.taragana.com/index.php/archive/wordpress-tip-on-permalink-options/

另一个现有解决方法是使用IIS的404重定向。

它要求你的网站服务器允许你添加一个404重定向,但是并不需要你安装任何第三方的mod_rewrite软件,也不要求你必须使用/index.php/... 作为你的静态链接构造的开头。

这里是安装文件和说明:

http://www.keyboardface.com/IIS-Permalinks/

Fixing Permalink Problems

解决 (.htaccess)生成的问题

你的WordPress安装过程中可能没有生成.htaccess文件或者没有添加新的规则到已有的.htaccess文件,有两个可能会造成这种情况。如果之前你所做的都不起作用那么请按下面的方法一步一步的做。

  1. "更改文件的权限:"你必须使用chmod命令 更改 .htaccess文件的权限为666,这样你就可以用template editor来编辑它,但是并不是必须这么做,因为这样以来,博客的任何可以编辑模板的用户都能编辑它了。所以你可以把权限改为660使他成为服务器可写,同时保持相同的限制。
  2. "服务器的限制:"你的托管商可能会封禁SERVER_SOFTWARE 这个变量,这会造成WordPress的.htaccess文件生成失败。如果你确定你的服务器运行的是Apache,你可以通过更改wp-includes/vars.php 文件强制WordPress相信你的服务器正在运行Apache。请按下面的步骤做更改。
    1. 用WordPress管理面板内置的文件编辑器打开wp-includes/vars.php。要找到这个面板,请先登录WordPress,点击“网站管理”,然后点击“文件”,滚动页面到底部并在标题为“其他文件”的输入框里输入wp-includes/vars.php
    2. 找到 $is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0;,并把它替换为// $is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0;
    3. 在下面添加一行// $is_apache = strstr($_SERVER['SERVER_SOFTWARE'], 'Apache') ? 1 : 0; and type in $is_apache = 1;

Permalinks, .htaccess, and MS Frontpage

A note about Microsoft Frontpage, many servers (shared and dedicated) maintained and built by various hosting companies come with mod_frontpage compiled with the apache mod_frontpage and in many cases with the Frontpage Server Extensions installed, on each virtual 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 mod_rewrite 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 MS Frontpage) - UNTIL - 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 MS Frontpage uses the .htaccess file (which the wordpress mod_rewrite rules must go into) for it's "publishing" and "web authoring" configuration. As soon as the wordpress mod_rewrite code is added to the file, two things happen - the permalinks don't work, and the MS Frontpage Server extensions become corrupted.

I have tried countless ways to get around this, including trying to use rewrite rules that "ignore" the %{HTTP_USERAGENT)% used by Frontpage, to using a second AccessFilename .wpaccess to the httpd.conf file, and a host of other things, and nothing worked so that a person would be able to both use MS Frontpage to manage the website and use the permalinks for wordpress at the same time.

Until Now.

The solution is acctually quite simple, and I kind of figured it out by accident.

If you are using or wish to use MS Frontpage along with wordpress you'll need to take the following simple steps on your server (or have your hosting company do it for you).

MS Frontpage creates the following directory `_vti_bin`

Nested within that it creates both `_vti_adm` and `_vti_aut`

In addition to in your website (or wordpress) root folder in all of those directories you will find additional `.htaccess` files.

In all three of these directories AND in the root directory, at the top of ALL of the `.htaccess` files you simply need to add one line -

`Options +FollowSymlinks`

There may or may not already be a line in each like `Options None`. Edit and save each `.htaccess` file and you're done. Now everyhting works perfectly, including MS Frontpage, AND the permalinks of your choosing.

A Final Note

On a personal note, I prefer to use Frontpage to manage/maintain sites, I've been using it since around '96, and by now, since I do most work on UNIX servers anyway I have it configured to use external editors for just about everything, including Zend Studio for php files, Bradbury TopStyle for stylesheets, Adobe ImageReady/Photoshop for images, etc. I'm more or less just using Frontpage as a convenient way to manage the site and access everything, etc. Then when I hit the "save" button in any of the other applications, they have Frontpage save my changes directly to the server, with no need to be FTP'ing files around, etc. It does help to get lots accomplished very quickly, and I was pretty bummed for the past year or so with the permalink frustration, since I was either needing to not use permalinks or not use Frontpage, or keep re-installing the FP extensions. At one point I found a way to make a .htaccess for my "running" site, but then change it to a FP .htaccess when I was doing any work (permalinks of course didn't work), either way it was a real pain.

This should work with most versions of FP and most of the unix versions of the extensions in use today.

--Chradil 17:24, 17 May 2006 (GMT)

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.

http://yourdomain.example.com/2005/10/4/article-about-joe-fred-sally-and-bog

Can result in:

http://yourdomain.example.com/2005/10/4/article-about-joe-fred-sally-and-bog

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 .htaccess file and add the following and save the file:

RewriteRule ^post/([0-9]+)?/?([0-9]+)?/?$ /index.php?p=$1&page=$2 [QSA]

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:

http://yourdomain.example.com/post/(the ID #)

It is also worth noting that most email software will not cut off URLs that have been delineated with angle-brackets (< and >), so when pasting URLs into emails, you should write them as so:

Read my blog post at <http://yourdomain.example.com/2005/10/4/article-about-joe-fred-sally-and-bog>


Fixing Other Issues

If your .htaccess 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 None in your Apache httpd.config file, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem. When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files. Example of enabled AllowOverride directive in httpd.config:
<Directory />
   Options FollowSymLinks
   AllowOverride All
</Directory>

You may also have to enable the AllowOverride directive in your DocumentRoot:

<Directory /var/www/html>
   # ... other directives...
   AllowOverride All
</Directory>
You may also have to change the AllowOverride settings for the site. This is surely the case when using Mac Osx Server, but might be likewise with other systems. Usually you can find the site configuration files in /etc/httpd/sites/
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 httpd.config 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:
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/
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 .htaccess file that WordPress generates. To fix it, delete the contents of your .htaccess file and re-create it.
  1. In the Control Panel, go to Manage > Files (More Info on Editing Files)
  2. Click the link to your .htaccess file to edit its contents
  3. Copy 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
  4. Delete all contents from your .htaccess file and click the Update File button.
  5. In the Control Panel, go to Options > Permalinks.
  6. Click the Update Permalink Structure button to freshly generate new rewrite rules for your permalinks.
  7. Test the results using a link that had previously broken.
  8. Add any manual htaccess entries back in your file
    (Place manual htaccess entries before the # BEGIN WordPress or after # END WordPress lines.)
You may also perform similar steps by deleting the .htaccess 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 .htaccess
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

让你的帖子有一个 .html 的后缀

有一个简单的方法可以通过使用上述的结构标签,使你的帖子以 .html 后缀结尾。Following the example used on properly terminating permalinks, you could have a page like http://yoursite.com/2006/01/01/happy-newyear.html with this rule:

/%year%/%monthnum%/%day%/%postname%.html

要注意的是,这并不能创建真正的.html 文件。这只是一种幻觉。 这样做并不能带来真正的利益。...有的人错误的认为这样可以更容易被搜索引擎搜索到,所以他们想将他们的permalinks模仿为其他的发布系统的文件。

External Resources