2

I really need some help redirecting dirty URLs to clean ones.

Dirty URL: creature.php?beast=

Clean URL: /mythical-creature/

Currently my .htaccess looks like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mythicalcreatureslist.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www.mythbeasts.com [NC]
RewriteRule ^(.*)$ http://www.mythicalcreatureslist.com/$1 [L,R=301]

RewriteRule ^menu/([A-Za-z0-9-+]+)/?$  menu.php?menu=$1  [NC,L]
RewriteRule ^menu_two/([A-Za-z0-9-+]+)/?$  menu_two.php?menu=$1 [NC,L]

RewriteRule ^mythical-creature/([A-Za-z0-9-+\'%]+)/?$  creature.php?beast=$1  [NC,L]

What this does is it makes the website show clean URLs when browsing which is great. However the old dirty URLs do not redirect when typed in the URL bar.

Example: Mongolian Death Worm http://www.mythicalcreatureslist.com/creature.php?beast=Mongolian+Death+Worm

I want it to redirect to: http://www.mythicalcreatureslist.com/mythical-creature/Mongolian+Death+Worm

This is causing duplicate content. I have tried:

RewriteRule ^/mythical-creature/([A-Za-z0-9-+\'%]+)/?$  http://www.mythicalcreatureslist.com/creature.php?beast=$1 [R=301,NC,L]

But what that did was to cause the dirty one to be used all the time.

I then flipped it round:

RewriteRule  ^creature.php?beast=([A-Za-z0-9-+\'%]+)/?$ http://www.mythicalcreatureslist.com/mythical-creature/$1  [R=301,NC,L]

But that just caused 404s whilst browsing and still did not redirect the old dirty URLs when typed in the address bar.

Egan7
  • 55
  • 2
  • 10

1 Answers1

1

The regex did not not match an empty query parameter value. Flip the + to a *

RewriteRule ^/creature.php?beast=([A-Za-z0-9-+\'%]*)/?$ http:// www.mythicalcreatureslist.com/mythical-creature/$1 [R=301,NC,L]

Notice the addition of a leading slash also

Kyle
  • 1,019
  • 1
  • 10
  • 19
  • Thanks Kyle, but unfortunately this has not made a difference. It still causes 404s whilst browsing and does not redirect the old dirty URLs when typed in the address bar. – Egan7 Jan 01 '13 at 12:55
  • @Egan7 is this url accessible directly : http://www.mythicalcreatureslist.com/mythical-creature/ Mongolian+Death+Worm – Kyle Jan 01 '13 at 12:58
  • What url do you see on the 404? – Kyle Jan 01 '13 at 13:00
  • was a leading slash missing? – Kyle Jan 01 '13 at 13:09
  • Hi Kyle, the 404 page is one designed by me when the no creature name can be found in the database. The creature name comes from the GET function (the part that precedes creature.php?beast= or /mythical-creature/). CODE:global $sel_beast; if (isset($_GET["beast"])) { $sel_beast = $_GET["beast"]; } else { $sel_beast = ""; } if ($sel_creature) {}else{header("Location: cannot-be-found.html");} – Egan7 Jan 01 '13 at 13:20
  • When I try your code Mongolian Death Worm (aka $sel_beast) for some reason can not be found in the database or the GET function isn't getting Mongolian Death Worm from the URL so a 404 is triggered. But still old dirty URLs don't redirect. – Egan7 Jan 01 '13 at 13:29
  • @Egan7 The code is looking for a query parameter which after the rewrite is not present. The rewrite is happening correctly yes? – Kyle Jan 01 '13 at 13:30
  • The leading slash did not seem to make a difference – Egan7 Jan 01 '13 at 13:30
  • Issue Solved: It looks like my php files on the menu pages were writing the new url to the creature page. The .htaccess was then rewriting this to the old url so that it can function to dynamically create the page. This was **not** redirecting so the new url would be visible in the browser. Struggling with further apache mod_rewrite code I used php to redirect any old urls to the new one. As you can see the Mongolian Death Worm link above now redirects to the new structure. – Egan7 Jan 01 '13 at 21:05