2013-08-01 100 views
1

我想造成這個URL重寫:URL重寫和正則表達式

test.php?site=hi --> www.domain.com/hi 
test.php?site=hi&id=1 --> www.domain.com/hi/1 
test.php?site=hi&cmd=test --> www.domain.com/hi/test 
test.php?site=hi&cmd=test&id=1 --> www.domain.com/hi/test/1 

我已經試過這一點,但它不工作。我是新的正則表達式,所以可能有更正的地方:

RewriteRule ^([a-zA-Z]+)/?+([0-9]+)?/? test.php?site=$1&id=$2 
RewriteRule ^([a-zA-Z]+)/?+([a-zA-Z]+)?/?+([0-9]+)?/? test.php?site=$1&cmd=$2&id=$3 

RewriteRule應該如何?

在此先感謝。

回答

1

啓用mod_rewrite的,並通過httpd.conf的.htaccess,然後把這個代碼在你.htaccessDOCUMENT_ROOT目錄:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

RewriteRule ^([^/]+)/([a-z]+)/(\d+)/?$ /test.php?site=$1&cmd=$2&id=$3 [L,NC,QSA] 

RewriteRule ^([^/]+)/([a-z]+)/?$ /test.php?site=$1&cmd=$2 [L,NC,QSA] 

RewriteRule ^([^/]+)/(\d+)/?$ /test.php?site=$1&id=$2 [L,QSA] 

RewriteRule ^([^/]+)/?$ /test.php?site=$1 [L,QSA] 
+0

運行完美,謝謝! – hskrijelj

+0

不客氣,很高興它解決了。 – anubhava