2012-07-19 72 views
0

好的,我在重写.htaccess文件中的规则时感到厌烦!对静态文件进行URL重写检查,然后重写为index.php

我期望的场景(使用URL http://doma.in/作为例子):

  • 首先检查以查看是否有index.html文件中的/public子目录是否存在;如果是这样的话,可以提供服务
    • 如果没有,发球(重写)index.php

要在我的例子扩大,说我们请求的URL http://doma.in/js/foobar.js

  • 首先检查,看是否foobar.js文件中的/public/js次目录的存在;如果是这样的话,可以提供服务
    • 如果没有,发球(重写)index.php?controller=js%2Ffoobar.js

将涵盖静态文件,但我还需要网址,像http://doma.in/foo

  • 首先检查,看是否foo文件中的/public次目录的存在;如果是这样的话,可以提供服务
    • 如果没有,发球(重写)index.php?controller=foo

和URL http://doma.in/foo/bar

  • 我们可以假设foo/bar不存在于/public子目录的文件不能被命名为喜欢该文件。
    • 所以服务(重写)index.php?controller=foo&action=bar

我敢肯定,如果这个复杂的(对我来说)位被盖住,那么我可以工作的查询串入的场合太多;所以http://doma.in/foo/bar/foo/bar将服务index.php?controller=foo&action=bar&querystring=foo%2Fbar

我也想确保一个斜线的处理一样,如果尾随斜线省略,例如:http://doma.in/foo/bar/foo/barhttp://doma.in/foo/bar/foo/bar/

我会从我的应用程序内处理404,就好像文件不存在,它会重定向到index.php这确实存在 - 我很高兴与此,除非你有更好的解决方案:)

我真的希望这一切都有道理,因为我一直在寻找解决方案到现在这一整天,这是我所有的:

Options +FollowSymLinks 

RewriteEngine on 

RewriteBase/

#RewriteBase /prompt-v3/ 
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$ 

RewriteCond $1 !^public 
RewriteRule ^(.*)$ public/$1 [R] 

注释掉的行在远程主机上处理子目录。到目前为止,我可以重定向到/public子目录,如果该文件存在那就是它。

谢谢大家的帮助!

回答

0
Options +FollowSymLinks 

RewriteEngine on 

RewriteBase/
#RewriteBase /sub-dir/ 
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$ 

RewriteRule ^index.html$ $1 [L,R=301] 

RewriteCond $1 !^public 
RewriteCond $1 !^lib 
RewriteRule ^(.*)$ public/$1 [L] 

RewriteCond $1 ^public/$ 
RewriteCond %{REQUEST_FILENAME}/index.html !-f 
RewriteRule ^(.*)$ lib/bootstrap.php [L] 

RewriteCond $1 !^public/$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond $1 ^public/(.*)$ 
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L] 

这将寻找在公共目录中的index.html文件,如果它不存在重写URL到LIB/bootstrap.php中。它实际上首先检查作为公共目录中的静态文件的任何请求并处理规范化。

0

将这个的.htaccess你的文档根

<ifModule mod_rewrite.c> 
    RewriteEngine on 

    # For /js/foobar.js (/js/*) 
    RewriteRule ^js/(.*)$ /public/js/$1 [NC] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L] 

    # For foo/bar (/*/*?*) 
    RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L] 
</IfModule>