简体   繁体   English

如何使用preg_replace替换另一个链接?

[英]How can I replace a link for another using preg_replace?

我正在尝试从链接中删除“ remove /”的操作

   preg_replace("https://mywebsite.com/remove/","https://mywebsite.com/")

You can use str_replace() . 您可以使用str_replace()

str_replace($search, $replace, $subject)

  • $search is the needle $search是关键
  • $replace is the replacement $replace是替换
  • $subject is the string we want to modify $subject是我们要修改的字符串

So in your case it would be: 因此,在您的情况下,它将是:

str_replace('remove/', '', 'https://mywebsite.com/remove/');

使用str_replace()

print(str_replace("/remove/", "", $link));

使用str_replace替换字符串中的特定部分。

echo str_replace("remove/", "", "https://mywebsite.com/remove/");

While @common-senses's answer will absolutely work - I'd like to propose another solution. 尽管@ common-senses的答案绝对有效,但我想提出另一种解决方案。 If your link to https://mywebsite.com/remove/ has already been indexed, you may want to use a 301 Redirect to your home page and/or issue a 410 "Gone" response on the removed page. 如果您对https://mywebsite.com/remove/的链接已被建立索引,则可能要使用301重定向到您的主页和/或在已删除的页面上发出410“已消失”响应。

If you're going to have a lot of resources that are moved/missing, you may want to use a plugin like Simple 301 Redirects . 如果您要移动/丢失大量资源,则可能需要使用诸如Simple 301 Redirects之类的插件。 Otherwise, you can simply add an .htaccess rule like 否则,您可以简单地添加.htaccess规则,例如

Redirect 301 /remove https://mywebsite.com/

Or add it via PHP if you're more comfortable with that 或者,如果您对此更满意,则通过PHP添加它

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: " . site_url() ); 
exit();

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

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