从内部URL获取数据 [英] Get data from within URL

查看:240
本文介绍了从内部URL获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,说test.com,如果有人访问test.com/hello我希望它重定向到test.com,并从后字符串传递数据的/(在这种情况下,你好)进入页面,例如通过PHP。

On my website, say test.com, if someone visits test.com/hello I want it to redirect to test.com and pass the string data from after the '/' (in this case "hello") into the page, for example through PHP.

我能做到这一点的,.htaccess文件?

Could I do this with a .htaccess file?

推荐答案

下面有2个选项来获得多个参数组合的mod_rewrite 和PHP code。 试试这个在根目录中选择一个.htaccess文件,使用该请求作为一个例子:

Here are 2 options to get multiple parameters combining mod_rewrite and PHP code. Try this in one .htaccess file at root directory, using this request as an example:

http://test.com/1/2/3

将下面的code在.htaccess文件中:

Put the following code in the .htaccess file:

# Rule-set starts with next 6 lines:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Exclude all requests to existing files.
RewriteCond %{REQUEST_FILENAME} -d  [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .*  -   [L]

1)选项使用变量 REQUEST_URI 在PHP中:

# Additional 2 lines in .htaccess file  
RewriteCond %{REQUEST_URI}  !Test\.php  [NC]
## Map silently all requests to Test.php
RewriteRule .*  /Test.php               [L]

PHP例如code。在的test.php 来捕捉URI路径/ 1/2/3:

PHP example code in Test.php to capture URI-path "/1/2/3":

$URI = explode('/', $_SERVER['REQUEST_URI']);
echo var_dump($URI);
/***
Result:
array (size=4)
  0 => string '' (length=0)
  1 => string '1' (length=1)
  2 => string '2' (length=1)
  3 => string '3' (length=1)
**/

2)选项使用变量 QUERY_STRING 在PHP中:

# Additional (Optional) 2 lines in .htaccess file  
RewriteCond %{REQUEST_URI}  !Test\.php  [NC]
## Map silently all requests to Test.php, passing the URI=Path as a query
RewriteRule ^(.*)  /Test.php?$1         [L]

PHP例如code。在的test.php 来捕获查询1/2/3:

PHP example code in Test.php to capture QUERY "1/2/3":

$QUERY = explode('/', $_SERVER['QUERY_STRING']);
echo var_dump($QUERY);
/***
Result:
array (size=3)
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '3' (length=1)
**/

这篇关于从内部URL获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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