简体   繁体   中英

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($search, $replace, $subject)

  • $search is the needle
  • $replace is the replacement
  • $subject is the string we want to modify

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. 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.

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 . Otherwise, you can simply add an .htaccess rule like

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

Or add it via PHP if you're more comfortable with that

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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