mod_rewrite前缀问题
我在使用 mod_rewrite 时遇到一些困难。我的目录结构(部分)是:
index.php
.htaccess
app
controllers
models
views
...
public
javascripts
stylesheets
foo
bar
因此,如您所见,所有静态文件都位于 app/public 中。这就是我想路由请求的原因,例如:
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,例如:
http://www.example.com/news/show/42 => index.php
(Because file app/public/news doens't exist)
所以,这是我当前的 .htaccess,但我认为它可以以更优雅的方式完成。
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 和样式表,而不处理所有其他文件。
我知道有:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
但是,我不知道如何使用前缀“app/public/”路由它们?我尝试过:
RewriteCond app/public/%{SCRIPT_FILENAME} !-f
RewriteCond app/public/%{SCRIPT_FILENAME} !-d
但是不行!我也尝试过使用RewriteBase,但没有成功。
编辑:
我正在使用 mod_expires,并且我真的很想自动化请求,例如:
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
因此,正如您所看到的,我希望在文件名和扩展名之间有可选的数字,并且应该忽略该数字(它仅用于将来的过期) )。
以前,我发现:
RewriteRule ^(.*)\.(\d+)(_m_\d+)?\.([^\.]+)$ $1.$4 [QSA]
但我不太明白它是如何工作的,它在我的示例中是否总是有效(可能有多个扩展)?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
/.htaccess
/app/.htaccess
/app/public/.htaccess
最后,创建一个 index.php 在您的 /app/public/ 中并在此处放置一个调度程序:
现在尝试访问:
Try this instead:
/.htaccess
/app/.htaccess
/app/public/.htaccess
Finally, create an index.php in your /app/public/ and put a dispatcher here:
Now say try accessing: