简体   繁体   English

WordPress .htaccess 重定向

[英]WordPress .htaccess Redirect

I normally wouldn't post on here and try to figure things out myself but I'm at my wits end.我通常不会在这里发帖并试图自己解决问题,但我已经无能为力了。

I'm trying to perform a redirect in WordPress so that requests to:我正在尝试在 WordPress 中执行重定向,以便请求:

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

... get forwarded onto a file called /property-details.php but the URL remains the same in the address bar. ... 转发到一个名为/property-details.php的文件,但地址栏中的 URL 保持不变。

Here's what I've tried:这是我尝试过的:

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

However, when I visit the URL /property/123-my-unique-uri-here I just got forwarded to the homepage.但是,当我访问 URL /property/123-my-unique-uri-here我只是被转发到了主页。

If anyone has any ideas at all I'd be eternally grateful!如果有人有任何想法,我将永远感激不尽!

try this: 尝试这个:

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

Another option is to just use a 301: 另一种选择是使用301:

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

The solution is to create a .htaccess rewrite rule that adds the trailing slashes to these urls. 解决方案是创建一个.htaccess重写规则,将尾部斜杠添加到这些URL。 Example - redirect all urls that do not have a trailing slash to urls with a trailing slash: 示例 - 将没有尾部斜杠的所有网址重定向到带有斜杠的网址:

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

Explanation of the add trailing slash .htaccess rewrite rule: 添加尾部斜杠.htaccess重写规则的说明:

The first line tells Apache that this is code for the rewrite engine of the mod_rewrite module of Apache. 第一行告诉Apache这是Apache的mod_rewrite模块的重写引擎的代码。 The 2nd line sets the current directory as page root. 第二行将当前目录设置为页面根目录。 But the interesting part is: 但有趣的是:

RewriteCond %{REQUEST_FILENAME} !-f

makes sure that existing files will not get a slash added. 确保现有文件不会添加斜杠。 You shouldn't do the same with directories since this would exclude the rewrite behavior for existing directories. 您不应对目录执行相同操作,因为这会排除现有目录的重写行为。 The line 这条线

RewriteCond %{REQUEST_URI} !example.php

excludes a sample url that should not be rewritten. 排除不应重写的示例网址。 This is just an example. 这只是一个例子。 If you do not have a file or url that should not be rewritten, remove this line. 如果您没有不应重写的文件或网址,请删除此行。 The condition: 条件:

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

finally fires when a url does not contain a trailing slash. 当url不包含尾部斜杠时终于触发。 Now we need to redirect the urls without the trailing slash: 现在我们需要重定向URL而不使用尾部斜杠:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM