mod_rewrite 前缀问题 [英] mod_rewrite prefix problem
问题描述
我在使用 mod_rewrite 时遇到了一些困难.我的目录结构(其中的一部分)是:
index.php.htaccess应用程序控制器楷模意见...民众javascripts样式表富酒吧
因此,如您所见,所有静态文件都在 app/public 中.这就是我想路由请求的原因:
http://www.example.com/images/logo.png =>应用程序/公共/图像/标志.pnghttp://www.example.com/javascripts/app.js =>应用程序/公共/javascripts/app.jshttp://www.example.com/uploads/blog/something =>应用程序/公共/上传/博客/东西
如果该文件不存在,那么它应该路由到 index.php,例如:
http://www.example.com/news/show/42 =>索引.php(因为文件 app/public/news 不存在)
所以,这是我目前的 .htaccess,但我认为它可以以更优雅的方式完成.
RewriteEngine onRewriteRule (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]重写规则 ^(.*)$ index.php [NC,L,QSA]
另外,这个问题是,它只处理图像、javascripts 和样式表,而不是所有其他文件.
我知道有:
RewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-d
但是,我不知道如何使用前缀app/public/"来路由它们?我试过了:
RewriteCond app/public/%{SCRIPT_FILENAME} !-fRewriteCond app/public/%{SCRIPT_FILENAME} !-d
但它不起作用!我也尝试过使用 RewriteBase,但没有成功.
我正在使用 mod_expires,我真的很想自动化请求,例如:
http://www.example.com/images/logo.42.png =>应用程序/公共/图像/标志.pnghttp://www.example.com/javascripts/app.42.js =>应用程序/公共/javascripts/app.js
所以,正如你所看到的,我希望在文件名和扩展名之间有一个可选的数字,这个数字应该被忽略(它只用于未来的到期).
之前,我发现:
RewriteRule ^(.*)\.(\d+)(_m_\d+)?\.([^\.]+)$ $1.$4 [QSA]
但我真的不明白它是如何工作的,它在我的例子中是否总是有效(可能有多个扩展)?
谢谢!
试试这个:
/.htaccess
RewriteEngine on重写规则 ^$ app/public/[L]RewriteRule (.*) app/public/$1 [L]
/app/.htaccess
RewriteEngine on重写规则 ^$ public/[L]RewriteRule (.*) public/$1 [L]
/app/public/.htaccess
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f重写规则 ^(.*)$ index.php?url=$1 [QSA,L]
最后,在您的 /app/public/ 中创建一个 index.php 并在此处放置一个调度程序:
<?php函数调度($action,$request){回声'动作:'.$动作;echo "\n参数:" ;打印_r($请求);}$fragments = expand('/', $_GET['url']);dispatch($fragments[0], array_slice($fragments, 1));?>
现在说尝试访问:
# 这些将通过 php/控制器/动作/发布/创建/发布/删除/1# 这些是静态文件/样式表//foo/bar/myfile.txt
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
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
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)
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]
Also, problem with this one is, it only handles images, javascripts and stylesheets, but not all other files.
I know there is:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
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
But it doesn't work! I also tried using RewriteBase, but it didn't work out.
EDIT:
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, 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)?
Thanks!
Try this instead:
/.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]
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>
Now say try accessing:
# these will go through php
/controller/action
/post/create
/post/delete/1
# these are static files
/stylesheets/
/foo/bar/myfile.txt
这篇关于mod_rewrite 前缀问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!