将动态 URL 转换为类似 url 的目录

发布于 2024-11-10 12:18:12 字数 1815 浏览 0 评论 0原文

的。我已经找到了大量关于转换特定网址字符串的资源(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷离° 2024-11-17 12:18:12

使用上面的 Nev Stokes 的 PHP 代码并尝试像这样创建 .htaccess 文件,这样它就不会破坏其他(静态)资源:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR] # 1. is this a request to static file ?
RewriteCond %{REQUEST_FILENAME} -l [OR] # 2. if not, is it a request to a symlink ?
RewriteCond %{REQUEST_FILENAME} -d      # 3. if not, is it a request to a directory ?
RewriteRule ^.*$ - [NC,L]               # 4. if true ( any of those three above ), serve the request normally and STOP ( because of L  `last` flag ).
RewriteRule ^.*$ index.php [NC,L]       # 5. if neither 1,2,3 are true, redirect to index.php

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:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR] # 1. is this a request to static file ?
RewriteCond %{REQUEST_FILENAME} -l [OR] # 2. if not, is it a request to a symlink ?
RewriteCond %{REQUEST_FILENAME} -d      # 3. if not, is it a request to a directory ?
RewriteRule ^.*$ - [NC,L]               # 4. if true ( any of those three above ), serve the request normally and STOP ( because of L  `last` flag ).
RewriteRule ^.*$ index.php [NC,L]       # 5. if neither 1,2,3 are true, redirect to index.php
我的影子我的梦 2024-11-17 12:18:12

如果你想重写一切,那么这真的很简单。

在您的 .htaccess 文件中:

RewriteEngine on
RewriteRule .* serve.php

在serve.php中,此代码将根据您的要求从带有键/值的URL创建一个数组:

if (preg_match_all('#([^/]+)(?:/([^/]+))?#', trim($_SERVER['REQUEST_URI'], '/'), $matches)) {
    $params = array_combine($matches[1], $matches[2]);
    var_dump($params);
}

If you want to rewrite everything then this is really quite simple.

In your .htaccess file:

RewriteEngine on
RewriteRule .* serve.php

In serve.php this code will create an array from the URL with key/value as you asked for:

if (preg_match_all('#([^/]+)(?:/([^/]+))?#', trim($_SERVER['REQUEST_URI'], '/'), $matches)) {
    $params = array_combine($matches[1], $matches[2]);
    var_dump($params);
}
通知家属抬走 2024-11-17 12:18:12

这是一个非常好的编写动态 url 的教程。并且您的特定问题已发布在那里。

.htaccess 技巧和提示

This is a very good tutorial for writing dynamic urls. and your particular problem is posted there.

.htaccess tricks and tips

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文