简体   繁体   English

使用htaccess在字符串的基础上重定向wordpress网站的url

[英]Redirect url of wordpress site on the basis of string using htaccess

I have a wordpress site eg. 我有一个wordpress网站,例如。 http://www.domain.com/ with custom theme hosted on a linux server and working fine. 具有自定义主题的http://www.domain.com/托管在linux服务器上,并且运行良好。 Some marketing strategy requires me to distinguish between 2 kinds of URLs (that they will be shooting from ads) and based on that redirect the site to different links. 某些营销策略要求我区分两种URL(它们将从广告中拍摄),并根据该URL将网站重定向到其他链接。

eg: 例如:

If the URL contains /xyz/ ie: 如果网址包含/ xyz /,即:
http://www.domain.com/xyz/category/post I want it to be redirected to an intermediate page ie: http://www.domain.com/intermediate.php and if the url doesnt contain /xyz/ ie: http://www.domain.com/xyz/category/post我希望将其重定向到一个中间页面,即: http : //www.domain.com/intermediate.php ,如果该网址不包含/ xyz /即:
http://www.domain.com/category/post the post should show up as usual. http://www.domain.com/category/post帖子应该照常显示。

I found out that this cant be done inside the wordpress code as before the first hook is triggered, the url is processed and a 404 page is thrown. 我发现无法像在触发第一个钩子,处理url并抛出404页之前那样在wordpress代码内完成此操作。

The only way I can achieve this is by modifying .htaccess file. 我可以实现的唯一方法是修改.htaccess文件。

I found a code which reads: 我发现一个代码如下:

RewriteRule ^(.*)/xyz/(.*)$ http://www.domain.com/intermediate.php [L,R=301]

and second way reads: 第二种方式是:

RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php

I am really confused about using it with the existing code in the htaccess file created by wordpress which reads: 我真的很困惑将它与wordpress创建的htaccess文件中的现有代码一起使用,该文件显示为:

# 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

I tried merging the codes ie: 我试图合并代码,即:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /domain/index.php [L]
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule  RewriteRule ^(.*)/xyz/(.*)$ http://domain.com/intermediate.php
</IfModule>

but only one of them work at a time ie: when the redirection works, wordpress posts doesnt show up and vice versa. 但是一次只能工作一个,即:当重定向有效时,wordpress帖子不会显示,反之亦然。 Kindly show me a better way. 请告诉我一个更好的方法。

Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this: 在“现有” WordPress代码中的RewriteEngine OnRewriteBase指令之后,立即尝试以下操作:

RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]

This will search for "xyz/" anywhere in the requested URL and redirect when found. 这将在请求的URL中的任何位置搜索“ xyz /”,并在找到时重定向。 Note that in .htaccess files, the directory prefix ( / in this case) is removed from the URL path before pattern matching. 请注意,在.htaccess文件中,在模式匹配之前,将从URL路径中删除目录前缀(在本例中为/ )。 So, a pattern that starts / will never match at the start of the URL. 因此,以/开头的模式永远不会在URL的开头匹配。

External redirects should generally come before internal rewrites, which is what the default WordPress directives do. 外部重定向通常应该在内部重写之前进行,这就是默认的WordPress指令所做的。

The alternative method you mention is less efficient: 您提到的另一种方法效率较低:

RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]

This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. 这将有效地执行相同的操作,但是由于通用的.*模式,将导致处理每个请求。 Note that the REQUEST_URI does start with a / (directory prefix). 请注意, REQUEST_URI 确实/ (目录前缀)开头。

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

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