Dan Hastings
Dan Hastings

Reputation: 3280

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]

Upvotes: 1

Views: 61

Answers (1)

anubhava
anubhava

Reputation: 785156

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]

Upvotes: 1

Related Questions