将动态 URL 转换为类似 url 的目录
的。我已经找到了大量关于转换特定网址字符串的资源(domain.com/?hello=world 到 domain.com/hello/world ),但我正在寻找的是一种动态转换我传递到目录结构的任何网址的方法,或者我应该说,如果您转到domain.com/hello/world,那么它会自动传递到我的php脚本:domain.com/?hello/world。我需要它是完全动态的,以便我发送的任何内容都会被转换。
即
domain.com/login/register被我的php脚本视为domain.com/?login=register 域名.com/登录 = 域名.com/?登录= 域名.com/hello/world = 域名.com/?hello=world 域名.com/pages/about = 域名.com/?pages=about domain.com/about =domain.com/?
about 更重要的是,我需要能够做到这一点..domain.com
/login/register/confirm/6473440367233483730126345 =domain.com/?login=register&confirm=6473440367233483730126345 domain.com/posts/categories/general = domain.com/?posts=categories&general
基本上每个奇数目录都是 get Key,每个偶数目录都是一个值(如果存在)。这需要根据需要持续进行,而不仅限于 2 或 3 个键/值字符串。
编辑:这是我最初想到的(最终解决方案见下文)。
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
index.php
$ReqURI = array_filter(explode('/',$_SERVER['REQUEST_URI']));
由于这里的一些线程和一些调整,这是最终的解决方案。
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
索引.php
if(preg_match_all('([^/]+)', $_SERVER['REQUEST_URI'], $matches)){
$val = array();
$key = array();
foreach ($matches[0] as $i => $req){
if($i % 2){
$val[] = $req;
}else{
$key[] = $req;
}
}
if(count($val) < count($key)){
$val[] = '';
}
$params = array_combine($key,$val);
print_r($params);
}
Of. I have found plenty of resources on converting specific url strings (domain.com/?hello=world to domain.com/hello/world ) but what I am looking for is a way to dynamically convert ANY url that I pass to a directory stucture, or I should say, if you go to domain.com/hello/world then it automatically passes to my php script: domain.com/?hello/world . I need this to be completely dynamic so that anything that I send will be converted.
i.e.
domain.com/login/register is seen by my php script as domain.com/?login=register
domain.com/login = domain.com/?login=
domain.com/hello/world = domain.com/?hello=world
domain.com/pages/about = domain.com/?pages=about
domain.com/about = domain.com/?about
and more importantly, I need to be able to do this..
domain.com/login/register/confirm/6473440367233483730126345 = domain.com/?login=register&confirm=6473440367233483730126345
domain.com/posts/categories/general = domain.com/?posts=categories&general
basically each odd directory is the get Key and each even is a value (if any is present). This needs to go on for as long as necessary and not be limited to only 2 or 3 key/value strings.
Edit: This is what I originally came up with (seee below for final solution).
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
index.php
$ReqURI = array_filter(explode('/',$_SERVER['REQUEST_URI']));
And this is the final solution thanks to a few threads here and some tweaking.
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
index.php
if(preg_match_all('([^/]+)', $_SERVER['REQUEST_URI'], $matches)){
$val = array();
$key = array();
foreach ($matches[0] as $i => $req){
if($i % 2){
$val[] = $req;
}else{
$key[] = $req;
}
}
if(count($val) < count($key)){
$val[] = '';
}
$params = array_combine($key,$val);
print_r($params);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用上面的
Nev Stokes
的 PHP 代码并尝试像这样创建 .htaccess 文件,这样它就不会破坏其他(静态)资源:Use
Nev Stokes
's PHP code from above and try to create the .htaccess file like this, so that it won't break other ( static ) resources:如果你想重写一切,那么这真的很简单。
在您的 .htaccess 文件中:
在serve.php中,此代码将根据您的要求从带有键/值的URL创建一个数组:
If you want to rewrite everything then this is really quite simple.
In your .htaccess file:
In serve.php this code will create an array from the URL with key/value as you asked for:
这是一个非常好的编写动态 url 的教程。并且您的特定问题已发布在那里。
.htaccess 技巧和提示
This is a very good tutorial for writing dynamic urls. and your particular problem is posted there.
.htaccess tricks and tips