2010-08-30 57 views
-2

重寫了我的網址。但是,我仍然可以訪問帶有問號和加號的重寫網址。上述需要重新查詢問號並在url中輸入等號

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

三個網址是完全一樣的頁面,我想使他們重定向到正確的結構(最後URL)來阻止網頁複製到正確地重新寫。

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

嘗試下列規則:

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] 

但我想最簡單的辦法是使用更強大的語言不是像PHP的mod_rewrite:

$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; 
} 
+0

的htacccess規則從當工作分得很開我試試這個例子: http://www.lovelakedistrict.com/lake-district-cottages/?cottages=2/ 它指向 http://www.lovelakedistrict.com/ lake-cottage-cottages.php/cottages/2/ (增加了php的擴展) - 可以刪除嗎?感謝您的快速回復 – AJFMEDIA 2010-08-30 15:36:07

+0

@ajfmedia:這可能是由於您使用這些規則的順序。請務必在那些導致內部重定向的規則之前放置那些導致外部重定向的規則。 – Gumbo 2010-08-30 15:46:00

+0

它確定它現在排序:)非常感謝 – AJFMEDIA 2010-08-30 15:47:32