2013-04-23 24 views
-2

我想如下重新寫一個網址...重新寫一個URL和存儲變量

例如輸入:

test.com/test/123/ 
test.com/test/123/456/ 
test.com/test/123/456/789 

輸出:

test.com/test/ 
test.com/test/ 
test.com/test/ 

但我想將結尾作爲變量存儲在test.com/test/頁面上的模板中

e.g

variable from input 1 : /123/ 
variable from input 2 : /123/456/ 
variable from input 3 : /123/456/789 

這可能嗎?

+0

是的,這是可能的。你有什麼想要做的? – Neal 2013-04-23 14:15:17

+0

是的,它肯定是可能 – MLeFevre 2013-04-23 14:16:09

+0

看的.htaccess和mod_rewrite的 – Waygood 2013-04-23 14:17:35

回答

1

以此爲起點:一是你的.htaccess接收URL,然後將其發送到PHP腳本:

RewriteEngine On 

#Skip virtual folders and files... 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

#Send the parameters to a php page 
RewriteRule ^/test/(.*)/?$ http://test.com/test/index.php?route=$1 [L,QSA] 

那麼php通過$ _GET

echo $_GET['route']; // '/123/456/789' 
得到的參數字符串

有了這個,你可以explode()/並分開變量。

好運。

+0

我試圖寫這樣的htaccess,但頁面不重定向。可以wordpress重寫這個?感謝 – scott 2013-04-23 14:43:59

+0

RewriteEngine敘述在 RewriteBase/ 重寫規則^索引\ .PHP $! - [L] 的RewriteCond%{REQUEST_FILENAME} -f 的RewriteCond%{REQUEST_FILENAME} -D- 重寫規則。的index.php [L] #BEGIN WordPress的 RewriteEngine敘述在 RewriteBase/ 重寫規則^索引\ .PHP $! - [L] 的RewriteCond%{REQUEST_FILENAME} -f 的RewriteCond %{REQUEST_FILENAME}!-d RewriteRule。的index.php [L] #Send參數的PHP頁面 重寫規則^/ABC /(.*)/?$ /test/index.php?route=$1 [L,QSA] #END WordPress – scott 2013-04-23 14:44:37

+0

與Wordpress一起使用可能會有危險。並嘗試把這個規則作爲第一 – 2013-04-23 15:55:10