2011-06-16 29 views
1

Hy!如何重寫我的網址?

我正在開發一個網站,並與.htaccess有一個小問題。

問題: 如何重寫url?

From: http://www.mysite.com/index.php?page=about 
To: http://www.mysite.com/about/ 

From: http://www.mysite.com/index.php?page=stuff&catId=1 
To: http://www.mysite.com/stuff/1/ 

From: http://www.mysite.com/index.php?page=stuff&catId=1#someAnchor 
To: http://www.mysite.com/stuff/1/#someAnchor 

目前,我這樣做,但它不工作! :(

<IfModule mod_rewrite.c> 
Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L] 
RewriteRule ^page/([^/.]+)/([^/.]+)/?$ index.php?page=$1&catId=$2 [L] 
</IfModule> 

請幫助。 在此先感謝,西爾瓦諾。

+0

爲什麼不把這些包含在你的PHP腳本中?您可以在請求特定頁面時重定向。 – Coeffect 2011-06-16 19:10:17

+1

因爲它對搜索引擎更友好 – human 2011-06-16 19:19:04

回答

2

這會做你想要什麼:

#Rewrite /about/ to /index.php?page=about 
RewriteRule ^([0-9a-z]+)/?$ index.php?page=$1 [NC,L] 

#/stuff/1/ to /index.php?page=stuff&catId=1 
RewriteRule ^([0-9a-z]+)/([0-9]+)/?$ index.php?page=$1&catId=$2 [NC,L] 

#/stuff/1/#someAnchor to /index.php?page=stuff&catId=1#someAnchor 
RewriteRule ^([0-9a-z]+)/([0-9]+)/(#[0-9a-z]+)?$ index.php?page=$1&catId=$2$3 [NC,L] 

作品在我的本地服務器(WAMP)上。

+0

對不起,它不起作用。我有一個「內部服務器錯誤」。其他一些想法?最好的祝福。 – human 2011-06-19 15:39:18

+0

對不起。我的錯。我的.htaccss中有一些未知的字符。感謝和最好的問候。 – human 2011-06-19 16:39:25

1
Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L] 
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?page=$1&catId=$2 [L] 
+0

謝謝,但它不起作用:/ – human 2011-06-16 20:23:13

+0

您是否在httpd.conf中有以下行? LoadModule rewrite_module modules/mod_rewrite.so – DENIEL 2011-06-17 13:02:51

0

你現在有什麼我的.htaccess文件不相關的改寫請求。

這裏是你需要:

第一張:

RewriteRule ^(.*)/$ index.php?page=$1 

第二:

RewriteRule ^(.*)/([0-9]*)$ index.php?page=$1&catId=$2 

一定要在頂部加入RewriteEngine on

您的服務器永遠不會獲得網址的「#someAnchor」部分,因爲世界上沒有任何瀏覽器會將此作爲請求的一部分發送。瀏覽器保持私密。

+0

謝謝,但您的解決方案也無法正常工作:/ – human 2011-06-16 20:23:29