mod_rewrite的preFIX问题 [英] mod_rewrite prefix problem
问题描述
我在使用mod_rewrite的一些困难。我的目录结构(它的一部分)是:
I'm having some difficulties with mod_rewrite. My directory structure (part of it) is:
index.php
.htaccess
app
controllers
models
views
...
public
javascripts
stylesheets
foo
bar
所以,你可以看到,所有的静态文件都在app /公众。这就是我喜欢它的原因路由请求,如:
So, as you can see, all static files are in app/public. That's the reason I would like to route requests like:
http://www.example.com/images/logo.png => app/public/images/logo.png
http://www.example.com/javascripts/app.js => app/public/javascripts/app.js
http://www.example.com/uploads/blog/something => app/public/uploads/blog/something
如果该文件不存在,那么它应该路由到index.php,如:
If that file doesn't exist, then it should route to index.php, like:
http://www.example.com/news/show/42 => index.php
(Because file app/public/news doens't exist)
所以,这是我目前的.htaccess,但我认为它可以更优雅的方式来完成。
So, this is my current .htaccess, but I think it can be done in more elegant way.
RewriteEngine on
RewriteRule (png|jpg|gif|bmp|ico)$ app/public/images/$1/$2.$3 [L]
RewriteRule ([^/.]+).js$ app/public/javascripts/$1.js [L]
RewriteRule ([^/.]+).css$ app/public/stylesheets/$1.css [L]
RewriteRule ^(.*)$ index.php [NC,L,QSA]
此外,问题与这一个时,它只能处理图像,JavaScript和样式表,但不是所有的其他文件。
Also, problem with this one is, it only handles images, javascripts and stylesheets, but not all other files.
我知道有:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
不过,我不知道如何将它们与preFIX应用程序/公/路线?我想:
But, I don't know how to route them with prefix "app/public/"? I tried:
RewriteCond app/public/%{SCRIPT_FILENAME} !-f
RewriteCond app/public/%{SCRIPT_FILENAME} !-d
不过,这是行不通的!我也尝试过使用的RewriteBase,但没有成功。
But it doesn't work! I also tried using RewriteBase, but it didn't work out.
编辑:
我使用mod_expires,我真的很想来自动请求,如:
I'm using mod_expires, and I'd really like to automate requests like:
http://www.example.com/images/logo.42.png => app/public/images/logo.png
http://www.example.com/javascripts/app.42.js => app/public/javascripts/app.js
所以,你可以看到,我想有文件名和扩展名之间的可选号码,而且这个数字应该被忽略(只用于未来到期)。
So, as you can see, I would like to have optional number between filename and extension, and that number should be ignored (it is only used for future expires).
previously,我发现:
Previously, I found:
RewriteRule ^(.*)\.(\d+)(_m_\d+)?\.([^\.]+)$ $1.$4 [QSA]
但我实在不明白它是如何工作?它总是在我的例子(也许多个扩展名)的工作?
But I don't really understand how it works and does it always work in my example (multiple extensions maybe)?
谢谢!
推荐答案
试试这个来代替:
/的.htaccess
RewriteEngine on
RewriteRule ^$ app/public/ [L]
RewriteRule (.*) app/public/$1 [L]
/app/.htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
/app/public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
最后,创建一个的index.php 在 / APP /公/ ,然后把一个调度员在这里:
Finally, create an index.php in your /app/public/ and put a dispatcher here:
<pre>
<?php
function dispatch($action, $request) {
echo 'action: ' . $action;
echo "\nparameters: " ;
print_r($request);
}
$fragments = explode('/', $_GET['url']);
dispatch($fragments[0], array_slice($fragments, 1));
?>
</pre>
现在说尝试访问:
# these will go through php
/controller/action
/post/create
/post/delete/1
# these are static files
/stylesheets/
/foo/bar/myfile.txt
这篇关于mod_rewrite的preFIX问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!