htaccess url参数使用index.php获取值

时间:2014-04-09 06:41:29

标签: php apache .htaccess mod-rewrite

`example.com/recent/index.php?page=2`  i want to like this -> `example.com/recent/page/2`

的.htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /recent/

RewriteRule ^([^/]+/([0-9]+)/?$ index.php?page=$2 [L,QSA]

的index.php?页= 2

<?php
echo $_GET['page'];
?>

如何为该htaccess文件创建。如何从最后一个网址example.com/recent/page/2

2 个答案:

答案 0 :(得分:1)

我们可以使用.htaccess文件重写网址。但在.htaccess文件中写入内容之前,我们需要验证我们使用的是哪种服务器。在这种情况下,您可以使用apache服务器或Nginx服务器。您只需添加

即可轻松查看
<?php phpinfo(); ?>

在php页面中。

案例1:Apache服务器:

确保在apache服务器中启用了mod_rewrite模块。这可以通过检查phpinfo包含的页面来识别。

用于网址重写的常用方法如下

<强> RewriteEngine叙述

这对任何apache服务器都是必需的。这用于启用重写引擎。在某些情况下,默认情况下应该打开。确保在htaccess文件之前必须强制使用以下代码

RewriteEngine On

<强>重写规则

在某些情况下,您可能只有几页,您可以在.htaccess文件中指定网站中的每个页面。在这种情况下,您可以使用这些重写 例如

RewriteRule ^ article / used / to / be / here.php $ / article / now / lives / here / [R = 301,L]

假设您必须像这些

重写网址
http://en.wikipedia.org/wiki/url_rewriting

您应该在.htaccess文件中写下类似的内容

RewriteEngine On
RewriteRule   ^wiki/(.+)$   w/index.php?title=$1   [L]

但是页面的实际实现将像这些

http://en.wikipedia.org/w/index.php?title=url_rewriting

<强>的RewriteCond

这可以用于根据某些条件重写某些URL。假设您需要使用您的网站名称添加或删除www,并在某些条件下重定向到某些页面,例如&#34; url不可用或错误消息&#34;

示例:

RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC] 

您可以在这里进一步参考

Introduction to url rewriting

Rewrite with examples

案例2:nginx服务器

在nginx服务器上启用模块HttpRewriteModule 使用位置根据nginx设置重写您的网址

示例

    location / {
    root   /path/to/drupal;
    index  index.php index.html;
 
    try_files $uri $uri/ @rewrite;
}
 
location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
}

答案 1 :(得分:0)

试试这个:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /recent/

RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?$1=$2 [L,QSA]
相关问题