htaccess url参数通过index.php获取值


htaccess url parameter get value with index.php

`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文件。如何从上一个url example.com/recent/page/2

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

<?php phpinfo(); ?>

在php页面中。

案例1:Apache服务器:

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

url重写常用的方法如下

重写引擎

这对于任何apache服务器都是强制性的。这用于启用重写引擎。在某些情况下,默认情况下它应该处于启用状态。因此,请确保以下代码必须是您的htaccess文件顶部的强制性代码

RewriteEngine On

重写规则

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

重写规则^article/uused/to/be/here.php$/article/now/lifes/here/[R=301,L]

假设你必须像这些一样重写url

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

重写结束

这可以用于根据某些条件重写某些url。假设你需要添加或删除带有你的网站名称的www,并在特定条件下重定向到特定页面,如"url不可用或错误消息">

示例:

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

你可以在这里进一步参考

url重写简介

重写示例

案例2:nginx服务器

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

示例

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

试试这个:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /recent/
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?$1=$2 [L,QSA]