0

我正在尝试向我的 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。

谢谢你的帮助!

4

2 回答 2

0

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

你不应该重复整个块。<IfModule mod_rewrite.c>当您添加另一个块时,Apache 可能(未验证)只是忘记了第一个 "块

我没有验证您的正则表达式,但它们的语法看起来不错

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>
于 2012-11-17T13:08:24.273 回答
0

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

我能够像这样删除这些参数:

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

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

于 2012-12-09T20:49:20.233 回答