9

我的 wordpress 博客安装在domain.com/blog,我有一个页面,其子页面的结构看起来像domain.com/blog/pagedomain.com/blog/page/subpage.

我希望我的访问者能够在不被外部重定向到该 URL 的情况下访问domain.com/subpage并查看内容domain.com/blog/page/subpage,避免 wordpress 永久链接重写。

我尝试使用RewriteRule ^subpage$ /page/subpage [L]并且正在提供内容,但 url 看起来像domain.com/blog/page/subpage(我猜 Wordpress 永久链接正在获取它。)

.ht 访问:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]

// tried inserting my code here.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

编辑:

这些日志显示页面访问活动 -

ip - - [19/Jun/2012:14:03:53 -0400] "GET /subpage/ HTTP/1.1" 301 - "http://domain.com/referrer/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1"
ip - - [19/Jun/2012:14:03:53 -0400] "GET /blog/page/subpage/ HTTP/1.1" 200 20022 "http://domain.com/referrer/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1"

另外,这是我的根 .htaccess -

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^subpage/?$ /blog/page/subpage/ [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
4

1 回答 1

3

Adding some rules in your htaccess, while using Wordpress, is always tricky. Instead, you should use its rewrite API.

First, put this code at then end of /wp-content/themes/CURRENT_THEME_ACTIVATED/functions.php:

function my_custom_page_rewrite_rule()
{
    add_rewrite_rule('^subpage/?', 'index.php?pagename=page/subpage', 'top');
}
add_filter('init', 'my_custom_page_rewrite_rule');

Remark: you need to specify the page levels in pagename parameter, otherwise the url will change.

Then, you need to tell Wordpress it has to take your new rule into consideration. For this, go to your admin panel: Settings > Permalinks > click on Save button. Now, you should be able to go to domain.com/blog/subpage and see the content of domain.com/blog/page/subpage (the url does not change anymore).

Finally, if you want to make domain.com/subpage reachable, you need to add a htaccess in root folder and put this code into it:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ blog/$1 [L]

And... that's it. You can go to domain.com/subpage and now you'll get what you want.

于 2016-04-06T16:26:56.190 回答