mod_rewrite前缀问题

发布于 2024-09-13 04:26:26 字数 1825 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-09-20 04:26:26

试试这个:

/.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/public/ 中并在此处放置一个调度程序:

<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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文