简体   繁体   中英

Remove parts of URL and redirect to specific page of site

I have a game site which recently migrated to wordpress, now I want to redirect the old URL to new one. Here's the example:

Old URL - https://www.website.com/fighting/stick-fighter.html New URL - https://www.website.com/stick-fighter/

I fixed this by creating this in htaccess:

Redirect 301 /fighting/stick-fighter.html /stick-fighter/

But my games are 500+ with different category so I don't want to encode each every game in htaccess. Is there a way to remove the category in URL which is "fighting" and remove the ".html" extension?

^(?<base>https?://www.website.com/)(?<catgory>fighting/)(?<page>.+?)(?<extension>\.html)$

Then combine "base" and "page" group

I am using C# like this:

string s1 = Regex.Replace(oldUrl, @"^(?<base>https?://www.website.com/)(?<catgory>fighting/)(?<page>.+?)(?<extension>\.html)$", @"$1$3/");

For the URL redirection, you should enable apache redirection by enabling "AllowOverride" in httpd.conf:

  • Change AllowOverride from None to all

     Options FollowSymLinks AllowOverride All 
  • Load redirect module

     LoadModule rewrite_module modules/mod_rewrite.so 
  • Change/Add rewrite rule

     <VirtualHost *:80> RewriteEngine On RewriteRule ^(?<base>https?://www.website.com/)(?<catgory>fighting/)(?<page>.+?)(?<extension>\\.html)$ $1$3/ [L] </VirtualHost> 

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