2014-11-04 108 views
2

如何使用.htaccess重寫規則顯示以下URL?參數?如何使用PHP中的.htaccess從URL中獲取參數

Original URL 
http://example.com/index.php?store=1 

Desired URL 
http://example.com/1/ 

OR

Desired URL 
http://example.com/store/1 

我嘗試以下在.htaccess代碼。我在這裏錯過了什麼?

RewriteEngine On 
Options +Followsymlinks 
RewriteCond %{REQUEST_FILENAME} !-f 
rewriteRule ^store/(.+)\$ index.php?store=$1 [L] 

我的index.php文件有這個代碼

<?php 
$storeid = $_GET['store']; 
echo $storeid; 
?> 

我是缺少在這裏?

回答

1

您正在轉義您的字符串結尾字符,導致表達式查找字面$符號,因此它永遠不匹配。

你需要的東西,如:

rewriteRule ^store/(.+)$ /index.php?store=$1 [L] 
        ^no back-slash here 

,或者如果你想確保你只匹配數字:

rewriteRule ^store/(\d+)$ /index.php?store=$1 [L]