简体   繁体   中英

Redirect a Mod_Rewrite URL using .htaccess

I have redesigned a website to get rid of the old ugly url formatting. The old URLs used mod_rewrite so i want to be able to redirect these urls so if a user clicks a link somewhere on the internet to an old url it will redirect to the new page.

The old url for a post would look like this

http://website.com/viewpost/123/review/review-title-slug.html

The new url will look like this

http://website.com/review-title-slug

The old mod rewrite rule looked like this.

RewriteRule ^viewpost/([^/]*)/([^/]*)/([^/]*)\.html$ /viewpost.php?ID=$1&type=$2&Title=$3 [L]

I was thinking i could add the following rule that might still catch the old urls but i cant get it to work. Even if it did work, it wouldnt be a redirect. I want it so if a user visits the old url it will redirect to the new url.

RewriteRule ^viewpost/([^/]*)/([^/]*)/([^/]*)\.html$ /$3 [L]

You can use this rule in root .htaccess of old site:

RewriteEngine On

RewriteRule ^viewpost/[^/]+/[^/]+/([^/]+)\.html$ /$1 [L,NC,R=301]

# comment out this older rule
# RewriteRule ^viewpost/([^/]*)/([^/]*)/([^/]*)\.html$ viewpost.php?ID=$1&type=$2&Title=$3 [L]

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