2009-07-08 105 views
1

谢谢大家的耐心和帮助。我完全重申了这个问题,因为我的所有修改都会变得很长。 我有4个的入口点的PHP MVC框架:URL重写帮助

从根:
的index.php
索引ajax.php
管理员/ index.php的
管理员/索引-ajax.php

我需要一个.htcaccess文件,它可以接受任何请求,并根据url将其重写到相应的文件中。长的网址是index.php?rt = cms/view/15,我希望它是index/cms/view/15。除了一个问题外,这一部分已经完成了。

这是我现在的.htaccess文件:

# htaccess file for framework - GOOD 
Options +FollowSymLinks 

# Turn on the mod_rewrite engine - GOOD 
RewriteEngine On 

# Hide indexes - GOOD 
Options -Indexes 

# If a file is not one of these, continue processing. - GOOD 
RewriteRule \.(css|js|jpg|jpeg|png|gif|ico)$ - [L] 

# RewriteRules for folder index files 
#RewriteRule ^(index)?(.php)?$ index.php [L] - GOOD 
#RewriteRule ^admin(/?)(index)?(.php)?$ admin/index.php [L] - GOOD 

# RewriteRules for admin folder arguements - going from more specific to less 
RewriteRule ^admin/ajax/[A-Za-z0-9-_/]*$ admin/index-ajax.php?rt=$1 [L] 
RewriteRule ^admin/[A-Za-z0-9-_/]*$ admin/index.php?rt=$1 [L] 

# RewriteRule for root ajax file 
RewriteRule ^ajax/[A-Za-z0-9-_/]*$ index-ajax.php?rt=$1 [L] 

# RewriteRule for root file - by here, it is not ajax or admin related, so only 
# possible option left if the root index file 
RewriteRule ^[A-Za-z0-9-_/]*$ index.php?rt=$1 [L] 

我做了一个简单的网站有两个文件夹 - “根”和“根/管理员”,而且每个的内部的CSS,图片,和一些虚拟内容的JavaScript文件夹。在'root'和'root/admin'里面有一个index.php和index-ajax.php文件,可以简单地输出任何url参数,并且使用来自每个文件夹的css,js和image文件。

我现在的问题是,如果我做一个像索引/等等或/管理/索引/等等的网址,那么该网页提出正确的参数是正确的。然而,当我做了像索引/等/视图或管理/索引/等/视图的网址然后争论是正确的(?rt = blah /视图),但页面呈现错误,因为css/js/images文件去索引/ blah/[css]而不是索引/ [css]。

有关如何处理此问题的任何想法?我允许css/js/image文件通过.htaccess原样通过,因此在那里的工作量会减少。

回答

0

所以图像/ CSS /等打破,因为你在HTML中使用相对URL,而现在你会更嵌套他们重新打破?在我遇到的一些其他MVC设置中,通常会有某种“根路径”变量,由Controller根据请求URL进行设置,并由View组件用于将所有路径前缀为图像/样式/脚本等资源。

将你的.htaccess问题变成一个Controller/PHP问题,如果这对你来说是一个可以接受的解决方案,那么你的Controller会做一些类似于“/”s上的请求URL的计算,计算结果片断的数量,并从结果中构建一个相对URL。

我为自定义MVC设置做了类似的事情;请查看http://avogadro.ws/hosted/skel/public/results/school-1并查看CSS位置的源代码。该页面的CSS实际上处于http://avogadro.ws/hosted/skel/级别,Controller将其构建为相对字符串。

2

那么,它看起来很不错,除了那些放在中间的$有点儿会毁了它;一个$标记正则表达式的结尾,所以我不认为它会正常工作。

此外,我认为您可能需要避开正斜杠,但我不确定。

+0

我只是不明显键入速度不够明显。 – seth 2009-07-08 21:50:22

+0

我认为$符号可作为占位符将来自左侧网址的值转移到正确的网址中? – 2009-07-08 21:52:34

+0

他意味着第一部分中的$,/ – seth 2009-07-08 21:53:36

0

最后一行应该有可能是:

RewriteRule ^/([A-Za-z\-_]+)/([A-Za-z\-_]+)/([A-Za-z0-9\-_]+)$ /index.php?com=$1&action=$2&val=$3 [L,R] 
9

你确定这一切都应该由index.php文件进行处理?什么静态文件,如图像/ CSS等?

这是另一种可能令你感兴趣的方法。您可以将任何尚不存在的URL作为文件或目录转发到index.php文件,然后解析其中的URL,例如, [domain.com]/cms/view/15将被重写为[domain.com] /index.php/cms/view/15。对于这项工作,您需要将apache指令AcceptPathInfo设置为On。

的.htaccess:

RewriteEngine On 
#check url is not a valid file 
RewriteCond %{REQUEST_FILENAME} !-f 
#check url is not a valid directory 
RewriteCond %{REQUEST_FILENAME} !-d 
#rewite anything left 
RewriteRule ^(.*)$ index.php/$1 [L] 

的index.php

$path = trim($_SERVER['PATH_INFO'], '/'); 
$pathParts = explode('/', $path); 

if (isset($pathParts[0])) { 
    $com = $pathParts[0]; 
} else { 
    $com = 'defaultcom'; 
} 

//$com[1] will be 'view' if supplied 
//$com[2] will be 15 if supplied 

我喜欢这种方法,因为你不会被强迫来定义和理解在Apache配置中的URL,但你可以完成PHP中的大部分工作。

编辑

你可以使用它作为你的.htaccess代替,这将与重定向列表中没有你的PHP脚本扩展的任何请求。 RewriteCond应该停止重写管理文件夹的请求。

RewriteEngine On 
#don't rewrite admin 
RewriteCond %{REQUEST_URI} ^admin/ 
#rewrite anything with a file extension not in the list 
RewriteRule !\.(js|gif|jpg|png|css|ico)$ /index.php [L] 
+0

图像和CSS也可以由PHP处理。我定期做一些非常愚蠢的事情,当然可以通过重新配置Apache来做到这一点(虽然你可能做不到,但可以):我把一个名为`static.php`的文件只添加过期头文件给`static_files /`。 – 2009-07-09 13:32:25

3

我会做like Tom Haigh said和解析与PHP请求的URL路径:

// extract the URL path 
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); 
// extract the path segments 
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/')); 
// expected parameters and its default values 
$params = array(
    'com' => 'default', 
    'action' => 'default', 
    'val' => 'default' 
); 
foreach ($params as $key => $val) { 
    if (isset($segments[0])) { 
     $_GET[rawurldecode($key)] = rawurldecode(array_shift($segments)); 
    } else { 
     break; 
    } 
} 
$restOfTheURLPath = implode('/', $segments); 
var_dump($_GET); 

而且mod_rewrite规则:

# redirect every request, that cannot be mapped to an existing file, to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^admin(/|$) admin/index.php [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

编辑在回答你的评论和现在编辑的问题:您可以排除目录你想在前面的是结束改写处理规则:

RewriteRule ^(css|images)/ - [L] 
RewriteRule ^admin/index\.php$ - [L] 
RewriteRule !^index\.php$ index.php [L] 
3

除非我错过了你的问题的要点(我在你的问题编辑后投入),这听起来好像你需要修复多个文件夹深度的URL的相对图像/ css路径。简单的解决方案,不要使用相对图像路径!只需用斜杠开始你的图像/ CSS链接..

<img src="/img/logo.png" alt="something" /> 

我一直认为这是一个更简单的方法,当你有一个模板供电很多页这不可预知的文件夹的深度。只要你有本地开发的测试URL,它对本地和生产服务器就能正常工作。使用Linux/Apache服务器

你的目录和文件设置进行测试时

1
# htaccess file for framework - GOOD 
Options +FollowSymLinks 

# Turn on the mod_rewrite engine - GOOD 
RewriteEngine On 

# Hide indexes - GOOD 
Options -Indexes 

# If a file is not one of these, continue processing. - GOOD 
RewriteRule ^images/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ - 
RewriteRule ^images/admin/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ - 
RewriteRule ^admin/(([A-Za-z0-9\-_/]*)/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ admin/images/$3.$4 [L] 
RewriteRule ^(([A-Za-z0-9\-_/]*)/([A-Za-z0-9\-_]*)\.(css|js|jpg|jpeg|png|gif|ico))$ images/$3.$4 [L] 

# RewriteRules for folder index files 
#RewriteRule ^(index)?(.php)?$ index.php [L] - GOOD 
#RewriteRule ^admin(/?)(index)?(.php)?$ admin/index.php [L] - GOOD 

# RewriteRules for admin folder arguements - going from more specific to less 
RewriteRule ^admin/ajax/([A-Za-z0-9\-_/]*)$ admin/index-ajax.php?rt=$1 [L] 
RewriteRule ^admin/([A-Za-z0-9\-_/]*)$ admin/index.php?rt=$1 [L] 

# RewriteRule for root ajax file 
RewriteRule ^ajax/([A-Za-z0-9\-_/]*)$ index-ajax.php?rt=$1 [L] 

# RewriteRule for root file - by here, it is not ajax or admin related, so only 
# possible option left if the root index file 
RewriteRule ^([A-Za-z0-9\-_/]*)$ index.php?rt=$1 [L] 

以上的.htaccess工作我代替你忽略图像与改写,使得在/ AJAX的图像的请求行/你好/世界/图像去/图像,如果它在管理文件夹,它将去/管理/图像