views:

55

answers:

1

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
+2  A: 

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
the htacccess rules work well apart from when I try this for examplehttp://www.lovelakedistrict.com/lake-district-cottages/?cottages=2/it directs tohttp://www.lovelakedistrict.com/lake-district-cottages.php/cottages/2/(adds on the php extention)- can this be removed? thanks for your quick reply
AJFMEDIA
@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
its ok its sorted now :) thanks alot
AJFMEDIA
One more thing Gumbo, is there a way I can stop the rules affecting anthing in the results directoryeg http://www.lovelakedistrict.com/result/q=windermere/ because it messes up my search query string :(- although it does tidy them up!
AJFMEDIA
@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
perfect thank you so much :)
AJFMEDIA
@ajfmedia: You’re welcome!
Gumbo
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