PHP(htaccess)用$ _GET请求重写

时间:2014-04-14 01:29:09

标签: php .htaccess mod-rewrite

我为我的网站开发制作了一个简单的框架。

我目前正在使用此网址格式:

www.example.com?r=account/login

但我想将其更改为:

www.example.com/account/login

从网址中删除r=

我见过YII框架就是这样做的。但我找不到办法做到这一点。

解决方案: 将以下代码添加到.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?r=$1
    RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?r=$1
</IfModule>`

2 个答案:

答案 0 :(得分:0)

您正在寻找的是SEO友好的URL,这是很常见的使用&amp;实行。这是来自基本WordPress安装的.htaccess,它实现了类似的目标:

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress

然后在index.php中,您可以通过执行$_GET的变量转储来测试这一点:

echo '<pre>';
print_r($_GET);
echo '</pre>';

答案 1 :(得分:0)

我看到你找到了一个解决方案但是你不需要/不应该为同一件事添加两个重写规则来添加/。您可以使用?使其成为可选项。我还建议在重写时使用[L],以便在满足规则时停止。

这更像是你在寻找的东西。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ /index.php?r=$1 [L]
</IfModule>
相关问题