2014-11-04 103 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]