如何在php中重写获取变量的URL [英] How to rewrite url for Get variable in php

查看:142
本文介绍了如何在php中重写获取变量的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从没有用过.htaccess是我的生活,现在我正在学习它,但是我发现很难理解。

I have never used .htaccess is my life now I am learning it but I find it very difficult to percieve.

谁能告诉我重写URL的步骤

Can anyone tell me steps to rewrite url

例如

从此

Www.example.com?s=abc

Www.example.com?s=abc

到此

Www.example.com/abc

Www.example.com/abc

推荐答案

.htaccess 实际上只是工作的一半。

The .htaccess is actually only 1 half of the job.

.htaccess 将实现您想要的(我认为)

This .htaccess will achieve what you want (I think)

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]

.htaccess 将所有请求转换为 index.php?request = myurl 在后台,而在最前面则显示 mydomain.com/myurl ,但是,对于第1至最后一行所述的扩展名,它不会这样做。 (否则,在尝试加载CSS,JS,图像等时会遇到麻烦)。添加任何您希望从那里的路由中排除的扩展。

This .htaccess turns all your requests into index.php?request=myurl on the background, while on the front it says mydomain.com/myurl, however, it will not do this for the extensions as stated on the 1-to last line. (otherwise you'll get into trouble when trying to load css, js, images, etc). Add any extension you wish to exclude from the routing there.

但是,仍然有一部分需要在PHP中捕获。 一切现在都会通过您的index.php路由。这意味着您将需要一个脚本来捕获请求并手动包含所需的文件。

However, there is still the part of catching this in PHP. Everything will route through your index.php now. This means you'll need a script to "catch" the request and manually include the files needed.

我将在此处为您粘贴一个非常简单的示例:



I'll paste a very simple example for you here:

<?php 
  $getRequest  = explode('/', $_GET['request']);
  $page = $getRequest[0];

  if( file_exists("/mypages/$page.php") ) {
    require_once('myheader.php');
    require_once("/mypages/$page.php");
    require_once('myfooter.php');
  }
  else{
   echo "page not found";
  }
?>

这篇关于如何在php中重写获取变量的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆