2011-01-05 24 views
0

我在我的CMS中生成了?pg = 1鏈接的分頁插件。我想在沒有這個醜陋的後綴的情況下重定向這個URL,因爲沒有它,CMS也會顯示第一頁。通過.htacess從URL刪除醜陋?pg = 1 RewriteRule

所以,我有一個像http://site.ru/category/schock-crane/?pg=1http://site.ru/moyka/?type=granit&pg=1 網址我想這樣的URL重定向到http://site.ru/category/schock-crane/http://site.ru/moyka/?type=granit respectly。

這個我試過的.htaccess代碼

RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L] 

我想在正則表達式培訓師這個正則表達式的代碼 - 它的工作。但是在服務器上不會發生重定向。

這裏是整個.htaccess文件:

AddDefaultCharset Off 
#DirectoryIndex index.php index.html 

Options +FollowSymLinks 
Options -Indexes 

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    # fix /?pg=1 
    RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L] 

    # fix .palitra-tab 
    RewriteCond %{REQUEST_URI} -tab$ 
    RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L] 

    RewriteCond %{REQUEST_URI} ^/csss/.* [NC] 
    RewriteRule csss/(.*)\.css$ css.php?n=$1 [L] 

    #RewriteRule ^(.*)/csss/(.*) /test.php [L] 

    RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2 

    #RewriteCond %{REQUEST_FILENAME} -f 
    #RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L] 

    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule ^(.+) - [PT,L] 

    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*) index.php 

    RewriteCond %{HTTP:Authorization} !^$ 
    RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}] 
</IfModule> 
+0

是遠程服務器上啓用'mod_rewrite'?如果刪除''條件會發生什麼? – 2011-01-05 01:10:39

+0

mod_rewrite工程 - CMS成功使用乾淨網址 – 2011-01-05 01:30:15

回答

0

您需要使用RewriteCond檢查查詢字符串:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$) 
RewriteCond %1%4 (.*?)&?$ 
RewriteRule^%{REQUEST_URI}?%1 [R=301,L] 

第二RewriteCond只是刪除尾隨&

+0

沒有工作 - 沒有URL重定向發生。嘗試沒有你的榜樣的第二行 - 也沒有成功。你可以在你的服務器上測試這個嗎? – 2011-01-07 14:30:27

+0

@the_ghost:我測試過它,它適用於測試用例'?pg = 1','?&pg = 1','?pg = 1&','?foo&pg = 1',??pg = 1&foo '。只要確保你將這條規則放在那些只是進行內部重寫的規則前面。 – Gumbo 2011-01-07 14:59:44

0

也許缺少^/殺死重定向

# fix /?pg=1 
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L] 

,或者嘗試

RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L] 
+0

沒有工作 - 沒有URL重定向發生 – 2011-01-07 14:29:30