从子目录运行Yii.删除子目录与.htaccess,而.css,.js和现有的文件和dirs仍然可用


Running Yii from subdir. Remove subdir with .htacces, while .css,.js and existing files and dirs still are available

我已经搜索了4个小时了,我仍然不能得到这个工作。我在我的web浏览器中有以下目录:

- application
    - assets
    - css
    - config
    - protected
        - .htaccess
    - ...
    - .htaccess
- framework
    - [Yii framework inside]
- .htaccess

只要请求的文件或目录不存在,我的浏览器中的。htaccess应该重定向/'重写'所有对xxx/xxx的请求到http://www.example.com/application/。这样,.css, .js和其他文件的请求仍然可以工作。

在我的配置中,我确保Yii期望SEO友好的URL,我不倾向于使用index.php。现在看起来是这样的:

'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            '' => 'site/index',
            '<controller:'w+>/<id:'d+>'=>'<controller>/view',
            '<controller:'w+>/<action:'w+>/<id:'d+>'=>'<controller>/<action>',
            '<controller:'w+>/<action:'w+>'=>'<controller>/<action>',
        ),
    ),
    'request' => array(
        'baseUrl' => 'http://www.example.com',
    ),

"请求"是后来添加的,因为Yii生成的链接为example.com/application/site/login。对于请求,Yii生成example.com/site/login。

在我的浏览器的。htaccess中,我尝试了所有的方法。

  • 首先,我能够从URL中"删除"子目录。显示索引页。
  • 我试图添加一个规则,使所有不存在的目录将被重定向到相同的url。
  • 我的第一条规则被打破了,'application'再次出现在URL中,但是没有加载css样式。
  • 在这一刻,我得到了索引页与css,但现在一切都把我带到索引页。

我的。htaccess是这样的:

RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^.*$ application/index.php

Mod_rewrite是启用的(我知道,因为有些东西以前工作过)。我看了ii文档中的例子。我尝试了Stack Overflow上其他问题的解决方案,比如这个问题和许多其他问题。还是没有运气

有人能帮我一下吗?


编辑:使用上面的。htaccess,对example.com的请求以example.com/application结束。然而,我想让"应用程序"再次"不可见"(以前工作过,不知道为什么它坏了)


我确实改变了我的。htaccess如下:

RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ application/$1 [L,QSA]

但是像www.brainfeeder.be/site/login这样的url仍然会把我带到默认的控制器/操作,即站点/索引。我想我的条件或规则还不完全正确。请查看我设置的小测试应用程序来解决这个问题。

发生了什么:brainfeeder。它被重写为brainfeeder。我的Yii应用程序在那里,所以它运行'bootstrap' index.php文件,并得到默认的控制器/动作,在本例中是site/index。现在,当你点击"login"按钮时,它应该会显示一个登录表单。但它停留在站点/索引视图


好的,我再次更新了我的。htaccess几次。现在我有以下情况:

  • www.example.com和example.com改写为www.example.com/application和example.com/application。
  • (www.)?example.com/existingfolder只显示'existingfolder'的内容。
  • (www.)?example.com/var1/var2/../varn重定向到(www.)?example.com/application/var1/var2/.../varn

现在我唯一希望发生的事情是后者被重写而不是重定向。所以访问者不知道他们在目录'application'。

所以(www.)?example.com/var1/var2/.../varn会把访问者直接带到正确的页面。

目前我的。htaccess的内容:

Options +FollowSymLinks
#IndexIgnore */*
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ /application/ [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [L]
# The directory or file does not exist
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /application/$1 [L,R]

事情是,当失去R标志在最后的RewriteRule将带我到index.php文件内的'应用程序',但它显示的主页而不是登录表单时,例如我去example.com/site/login。

我猜,脚本没有看到变量。(如果它做网站/错误将触发)所以它处理这个请求example.com/application,而不是example.com/application/var1/var2

我希望这次我把问题解释得更好。谢谢你一直以来的帮助。

如果您只想重写所有不存在的/$var1/$var2/application/:

,请尝试检查这些配置指令。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/

现在,如果您希望重定向那些不存在的文件,而不是重写它们?然后只需在RewriteRule指令的末尾添加一个[R]标志,只是不要忘记在标志之前添加一个" "空格。


现在,如果你想重定向/application/,然后重写/index.php/application,并且重写不存在的/$var1/$var2/application/$var1/$var2,那么这是相当困难的(并且需要一些确切的细节),但是你仍然可以尝试这些配置指令:
RewriteEngine on
# rewrite index.php
RewriteRule ^index.php$ /application
# rewrite unexisting files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /application/$1/$2
# try to remove this if it causes redirection loop
RewriteRule ^application/?$ / [R]

你也可以尝试在这些指令的最上面使用DirectoryIndex application/index.php来改变你的网站的索引,并删除RewriteRule ^index.php$ /application行,如果它导致错误。

事实上,我不明白你的问题。你说:

.htaccess在我的浏览器应该重定向/'重写'所有的请求无论xxx/xxx到http://www.example.com/application/,只要请求的文件或目录不存在。这种方式要求.css, .js和其他文件仍然可以工作。

现在,你对你的评论说:

任何到brainfeeder的链接。应该显示/application/$var1/$var2brainfeeder.be/var1/美元var2

如果您还想重定向现有的/application/$var1/$var2/$var1/$var2,那么请尝试添加这些指令,如果它导致系统错误,请删除它:

# the condition is important as mentioned above
RewriteCond %{REQUEST_URI} !'.css$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]

您可以在RewriteRule的顶部添加另一个条件(您喜欢多少就多少),如果您是程序员,请使用您的思维。如果您不想重定向扩展名为.jpg的文件,您可以添加另一个条件,如RewriteCond %{REQUEST_URI} !'.jpg$,或者您想要这样的:

RewriteCond %{REQUEST_URI} !'.css$
RewriteCond %{REQUEST_URI} !'.jpg$
RewriteCond %{REQUEST_URI} !'.png$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]

请尝试更改的来源。使用以下代码访问文件:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www'.)?brainfeeder'.be$
RewriteRule ^/?$ /application/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /application/$1 [L]

不知道你的问题是什么…

但是这里有一个。htaccess可以完成你想要的:

RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

你不需要编辑。htaccess。您只需要将Yii条目脚本(index.php)和默认的.htaccess从子目录移动到webroot(这样它们就直接位于public_html下)。将index.php和.htaccess移到根目录后,所有web请求将直接路由到webroot下的index.php(而不是子目录),从而消除了url的/子目录部分。

移动文件后,您将需要编辑index.php以更新对Yii .php文件(在Yii框架目录下)以及Yii配置文件(main.php)的引用。最后,您需要将assets目录直接移动到webroot中,因为默认情况下,Yii期望assets目录位于与入口脚本相同的位置。

这应该是所有你需要做的,但如果你需要更多的细节,我在这里描述更完整的方法:

http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory