WordPress .htaccess重定向

时间:2016-05-31 13:38:25

标签: php wordpress .htaccess

我通常不会在这里发帖并尝试自己解决问题但是我的智慧结束了。

我正在尝试在WordPress中执行重定向,以便请求:

/property/123-my-unique-uri-here

...转发到名为/property-details.php的文件,但网址在地址栏中保持不变。

这是我尝试过的:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^property/(.*)$ /wp-content/themes/Divi/property-details.php?uri=$1 [QSA,L]
</IfModule>

但是,当我访问网址/property/123-my-unique-uri-here时,我刚转发到主页。

如果有人有任何想法,我会永远感激不尽!

2 个答案:

答案 0 :(得分:0)

试试这个:

RewriteRule ^property/123-my-unique-uri-here$ /property-details.php?&%{QUERY_STRING}

另一种选择是使用301:

RedirectMatch 301 /property/123-my-unique-uri-here(.*) /property-details.php$1

答案 1 :(得分:0)

解决方案是创建一个.htaccess重写规则,为这些网址添加尾部斜杠。示例 - 将没有尾部斜杠的所有网址重定向到带有斜杠的网址:

RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} !example.php
        RewriteCond %{REQUEST_URI} !(.*)/$
        RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

添加尾随斜杠.htaccess重写规则的说明:

第一行告诉Apache这是Apache的mod_rewrite模块的重写引擎的代码。第二行将当前目录设置为页面根目录。但有趣的是:

RewriteCond %{REQUEST_FILENAME} !-f

确保现有文件不会添加斜杠。你不应该对目录做同样的事情,因为这会排除现有目录的重写行为。这条线

RewriteCond %{REQUEST_URI} !example.php

排除不应重写的示例网址。这只是一个例子。如果您没有不应重写的文件或网址,请删除此行。条件:

RewriteCond %{REQUEST_URI} !(.*)/$

当url不包含尾部斜杠时终于触发。现在我们需要重定向没有尾部斜杠的网址:

RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]