Jub*_*air 2 php apache mod-rewrite
的.我已经找到了大量有关转换特定网址字符串的资源(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 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
更重要的是,我需要能够做到这一点..
domain.com/login/register/confirm/6473440367233483730126345 = domain.com/?login=register&confirm=6473440367233483730126345 domain.com/posts/categories/general = domain.com/?posts=categories&general
基本上每个奇数目录都是get Key,每个even都是一个值(如果有的话).这需要在必要时继续进行,而不仅限于2或3个键/值字符串.
编辑:这是我最初想出的(见下面的最终解决方案).
的.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Run Code Online (Sandbox Code Playgroud)
的index.php
$ReqURI = array_filter(explode('/',$_SERVER['REQUEST_URI']));
Run Code Online (Sandbox Code Playgroud)
这是最终的解决方案,这要归功于一些线程和一些调整.
的.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]
Run Code Online (Sandbox Code Playgroud)
的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);
}
Run Code Online (Sandbox Code Playgroud)
使用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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
934 次 |
| 最近记录: |