Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据URL中的get变量重定向URL_Php_Regex_.htaccess - Fatal编程技术网

Php 如何根据URL中的get变量重定向URL

Php 如何根据URL中的get变量重定向URL,php,regex,.htaccess,Php,Regex,.htaccess,我正在尝试根据get变量更改我的网站URL,以便提高网站的安全性 例如,我想更改我的地址,这是我的htaccess文件: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1 我的网址是: http://joinexam.in/cat

我正在尝试根据get变量更改我的网站URL,以便提高网站的安全性

例如,我想更改我的地址,这是我的htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
我的网址是:

http://joinexam.in/category.php?cat_id=17&mode=full&start=36 
我想将此URL转换为:

http://joinexam.in/category/1736
  • 其中cat_id=17
  • 和开始=36
因此,在
category.php
页面之后,它将变成
1736
,我正在尝试使用.htaccess文件来实现它

这里我想把
cat_id
start
都作为get变量,然后根据这些get变量,我想更改我网站的URL


有人能解释一下实现这一目标的正确方法吗?

.htaccess

这会将每个URL转发到index.php

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php
PHP

获取index.php中的“原始”URL:

// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI']; 

// split the path by '/'
$params     = split("/", $request_uri);
然后您可以基于这些$params进行路由。 作为一个好的快速路由器库,我建议: 通过使用它,您不必乱搞htaccess regexp的东西,并且可以将东西保持在PHP级别:)