-2

Rewritten my url. However I can still access rewritten urls with question marks and plus signs.

lovelakedistrict.com/lake-district-cottages/?cottages=2/
lovelakedistrict.com/lake-district-cottages/?cottages/2/
lovelakedistrict.com/lake-district-cottages/cottages/2/

The three urls above are the exact same page, I would like to properly re-write them so they redirect to the correct structure (the last url) to stop duplication of webpages.

Options +FollowSymlinks
Options +Indexes
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/

RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
Cœur
  • 37,241
  • 25
  • 195
  • 267
AJFMEDIA
  • 2,093
  • 6
  • 29
  • 52

1 Answers1

2

Try these rules:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]

But I guess the easiest would be to use a more powerful language than mod_rewrite like PHP:

$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
if (count($parts) === 2) {
    $path = rtrim($parts[0], '/');
    parse_str($parts[1], $params);
    foreach ($params as $key => $value) {
        $path .= '/' . (($value === '') ? trim($key, '/') : trim($key, '/') . '/' . trim($value, '/'));
    }
    header('Location: http://example.com'.$path.'/', true, 301);
    exit;
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • the htacccess rules work well apart from when I try this for example http://www.lovelakedistrict.com/lake-district-cottages/?cottages=2/ it directs to http://www.lovelakedistrict.com/lake-district-cottages.php/cottages/2/ (adds on the php extention)- can this be removed? thanks for your quick reply – AJFMEDIA Aug 30 '10 at 15:36
  • @ajfmedia: That might be due to the order you use these rules. Make sure to put those rules, that cause an external redirect, before those rules, that just cause an internal redirect. – Gumbo Aug 30 '10 at 15:46
  • One more thing Gumbo, is there a way I can stop the rules affecting anthing in the results directory eg http://www.lovelakedistrict.com/result/q=windermere/ because it messes up my search query string :(- although it does tidy them up! – AJFMEDIA Aug 30 '10 at 16:02
  • @ajfmedia: Yes, you could either add the following condition to each rule: `RewriteCond %{REQUEST_URI} !^/result/`. Or you put this rule in front of the other rules to skip them: `RewriteRule ^result/ - [S=4]`. – Gumbo Aug 30 '10 at 17:13
  • Hey Gumbo, sorry to bother you, can you look at something else for me?- I didnt want to post a new question because I feel like they are all to much of the same. – AJFMEDIA Aug 31 '10 at 11:52