Mod rewrite Mod重写到子目录
我在以下方面遇到了一些问题:Mod rewrite Mod重写到子目录,mod-rewrite,Mod Rewrite,我在以下方面遇到了一些问题: <IfModule mod_rewrite.c> DirectoryIndex index.php Options +FollowSymLinks RewriteEngine On # If the file is not found in web RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem. Rewri
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [L]
# Then rewrite it to index.php
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
但是,我无法正确地获取静态资源,它们都被路由到index.php
我还想保持所有的网址相对;因此,我可以在不编辑代码的情况下使代码可重用
如果我访问img.gif,则以下.htaccess提供了一个内部服务器错误:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
#RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [L]
# Then rewrite it to index.php
#RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
DirectoryIndex.php
选项+FollowSymLinks
重新启动发动机
#如果在web中找不到该文件
#重写Cond web/$1-f[NC]#好的,我知道了:
当您进行重写时,请记住,重写的URL是在内部调用的。
因此,您基本上必须使用不同的值重新处理.htaccess
因此,在您的示例中:http://localhost/img.gif
指向http://localhost/web/img.gif
然后转到http://localhost/index.php
根据最后一条规则
我会尝试这样做(将最后一条规则替换为:)
(注意:[QSA]不需要,因为您不接触查询字符串,所以它按原样传递。)
编辑:那怎么办
# If the file is not found in web
RewriteCond web/$1 !-f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/
#Rewrite it to index.php
RewriteRule ^(.*)$ index.php [R,L]
# If the file is found in web
RewriteCond web/$1 -f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/
#Then point to web/image.gif
RewriteRule ^(.*)$ web/$1 [L]
请给我一些建议。这是我第一次没有得到答案。你所说的静态资源是什么意思?谢谢。因为你的答案是唯一的,所以即使它实际上不起作用,我也会把它打对。我用更多的数据更新了我的问题。如果你想给我任何建议,就说吧。
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
#RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [R]
# Then rewrite it to index.php
#RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
RewriteCond %{SCRIPT_NAME} !^/web/
RewriteRule ^(.*)$ index.php [L]
# If the file is not found in web
RewriteCond web/$1 !-f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/
#Rewrite it to index.php
RewriteRule ^(.*)$ index.php [R,L]
# If the file is found in web
RewriteCond web/$1 -f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/
#Then point to web/image.gif
RewriteRule ^(.*)$ web/$1 [L]