BerrKamal
BerrKamal

Reputation: 51

Redirect duplicate url in htaccess

I have duplicate urls and i need to do some redirection in htaccess

Examples:

Original Urls are :

www.mywebsite.com/listofgames/pacman.html  
www.mywebsite.com/listofgames/nintendo.html  
www.mywebsite.com/listofgames/snake.html  

I have about 1000 games name

Example of duplicate urls :

www.mywebsite.com/listofgames/1/pacman.html
www.mywebsite.com/listofgames/1521/pacman.html
www.mywebsite.com/listofgames/52154/pacman.html
www.mywebsite.com/listofgames/abc/pacman.html
www.mywebsite.com/listofgames/opq/pacman.html
www.mywebsite.com/listofgames/opq/1/2/35/pacman.html
www.mywebsite.com/listofgames/154/3562/snake.html
www.mywebsite.com/listofgames/2541/snake.html
www.mywebsite.com/listofgames/524/snake.html
www.mywebsite.com/listofgames/absc/gtar/15/nintendo.html
www.mywebsite.com/listofgames/nije/nintendo.html

I need to redirect them to original urls

So

www.mywebsite.com/listofgames/anynumber/mygamesname.html  
www.mywebsite.com/listofgames/anyword/mygamesname.html  
www.mywebsite.com/listofgames/anyword/anynumber/anyword/mygamesname.html

Must be redirected to

www.mywebsite.com/listofgames/mygamesname.html

Upvotes: 1

Views: 1627

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

You may try this:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/listofgames/.*/([^\.]+)\.html/?  [NC]
RewriteRule .*    listofgames/%1.html                    [R=301,L]

Redirects permanently

http://www.mywebsite.com/listofgames/any/number/of/folders/mygamesname.html

with or without trailing slash.

To:

http://www.mywebsite.com/listofgames/mygamesname.html

Effectively removing the segment path between /listofgames/ folder and the HTML file.

String listofgames and extension html are assumed to be fixed, while mygamesname is assumed to be variable.

The incoming URL structure has to be kept for the rule-set to work: Last string inside the URL must be the HTML file.

For silent mapping, remove R=301 from [R=301,L]

Upvotes: 1

Related Questions