使用htaccess覆盖WordPress永久链接结构

时间:2012-11-17 06:06:16

标签: wordpress .htaccess mod-rewrite

我正在尝试为我的htaccess文件添加一些重写规则,但我似乎无法绕过永久链接结构。这是我正在尝试的:

Options +FollowSymLinks

# BEGIN MyRules

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^properties/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ ?property=$1&page=$2
RewriteRule ^properties/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ?property=$1&page=$2
RewriteRule ^agents/(.*)$ ?agent=$1
RewriteRule ^agents/(.*)/$ ?agent=$1
</IfModule>

# END MyRules

# 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

WordPress不断将“属性”和“代理”呈现为类别,但由于这些类别不存在,我收到404错误。这些实际上是页面,我想将传递给这些页面的参数重写为url。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我已更正您的规则并添加了评论

你不应该重复整个街区。 Apache可能(没有验证)在添加另一个

时忘记第一个“<IfModule mod_rewrite.c>块”

我没有验证你的正则表达式,但他们看起来很好的语法

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    # enable the rewrite engine
    RewriteEngine On
    # "/" is the base path to work on
    RewriteBase /

    # dont rewrite calls on index.php
    RewriteRule ^index\.php$ - [L]

    # rewrite the url to index.php and pass query values
    # [L] indicates, that apache does not need to care 
    # about the following rewrites when this matches
    RewriteRule ^properties/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$  /index.php?property=$1&page=$2 [L]
    RewriteRule ^agents/(.*)/?$ /index.php?agent=$1

    # If the request does not point to a existing file ( image.jpg )
    RewriteCond %{REQUEST_FILENAME} !-f
    # If the request does not point to a existing directory ( /, default index.php )
    RewriteCond %{REQUEST_FILENAME} !-d
    # in this cases you would usually say "404 not found" but no, 
    # please direct all those to the index.php which will care of the rest
    RewriteRule . /index.php [L]
</IfModule>

答案 1 :(得分:0)

似乎WordPress已经将其他参数传递给索引,例如“name”和“category”,因为我正在尝试重写的结构类似于我正在使用的自定义永久链接结构。

我能够删除这些参数:

RewriteRule ^properties/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$  /index.php?property=$1&page=$2&cateogry=&name= [L]

米歇尔上面的建议并没有错,但这特别解决了我的问题。