2012-09-21 170 views
4

任何人都可以請幫我一些URL重寫?.htaccess友好URl

我: (示例)

www.example.com/index.php?page=namepage 
www.example.com/index.php?page=gallery&topic=nametopic 
www.example.com/index.php?page=homepage&paging=1 

我想有:

www.example.com/namepage 
www.example.com/gallery/nametopic 
www.example.com/homepage/1 

我在htaccess文件:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/?$ ?page=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/([^/]+)?/?$ ?page=$1&topic=$2 

但它不很努力嗯,因爲我可以寫:

  • www.example.com/index.php?page=namepage(頁或其他)
  • www.example.com/?page=namepage(頁或其他)
  • www.example.com/namepage/
  • www.example.com/namepage(這是我想要的 - 沒有其他人)

而第二個問題是:

  • www.example.com/namepage(OK,我想,我們見名稱)
  • www.example.com/namepage/whatever(NO OK,我想404,但是我們看到namepage)
  • www.example.com/gallery/topic(OK,我想,我們看到nametopic)
  • www.example.com/whatever/whatever2/whatever3(好吧,我想要404)

非常感謝任何人。

回答

7
### all your redirects 

# for www.example.com/index.php?page=homepage&paging=1 
RewriteCond %{THE_REQUEST} \?page=([^&]+)&paging=([0-9]+) 
RewriteRule^/%1/%2? [L,R=301] 

# for www.example.com/index.php?page=gallery&topic=nametopic 
RewriteCond %{THE_REQUEST} \?page=([^&]+)&topic=([^&\ ]+) 
RewriteRule^/%1/%2? [L,R=301] 

# for www.example.com/index.php?page=namepage 
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\) 
RewriteRule^/%1? [L,R=301] 

# for www.example.com/namepage/ 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

### all your rewrites back 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&paging=$2 [L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&topic=$2 [L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)$ /index.php?page=$1 [L] 
+0

絕對完美!非常感謝。 Bzzz,腦溢血。 –