1

I've been searching for 4 hours by now and I still can't get this to work. I have the following directories in my webroot:

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

The .htaccess in my webroot should redirect/'rewrite' all requests for whatever xxx/xxx to http://www.example.com/application/ as long as the requested file or directory does not exist. This way requests for .css, .js and other files can still work.

In my config I made sure Yii expects SEO friendly URL's and I don't tend to use the index.php. So it now looks like this:

'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',
    ),

The 'request' was added later on because Yii was generating links as example.com/application/site/login. With the request Yii generates example.com/site/login instead.

In the .htaccess in my webroot I tried about everything.

  • First I was able to 'remove' the subdir from the URL. The index page was shown.
  • I tried to add a rule so all none existing directories would be redirected to the same url.
  • My first rule broke, and 'application' was in the URL again, but no css styles were loaded.
  • At this moment I got the index page with css, but now everything brings me to the index page.

my .htaccess looks like this:

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 is enabled (I know because some things worked before). I looked at examples from the Yii docs. I tried solutions from other questions on Stack Overflow like this one and many many others. Still no luck.

Could someone please help me out?


edit: With the .htaccess above a request to example.com ends at example.com/application .. I however would like to make the 'application' 'invisible' again (worked before, don't know why it broke)


I did change my .htaccess as follows:

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]

But still a url like www.brainfeeder.be/site/login brings me to the default controller/action which is the site/index. I guess my conditions or rules are not exactly correct yet. Please see my small test application I set up to tackle this issue.

What happens: brainfeeder.be get rewritten to brainfeeder.be/application/ My Yii app is in there so it runs the 'bootstrap' index.php file and gets to the default controller/action, in this case site/index. Now when you click the 'login' button it should show you a login form. But it stays at the site/index view.


Ok, once again I updated my .htaccess a couple of times. Now I have the following situation:

  • www.example.com AND example.com are rewritten to www.example.com/application AND example.com/application.
  • (www.)?example.com/existingfolder just shows content of 'existingfolder'.
  • (www.)?example.com/var1/var2/../varn get redirected to (www.)?example.com/application/var1/var2/.../varn

Now the only thing I would like to happen is that the latter gets rewritten instead of redirected. So visitors don't know they are in the directory 'application'.

So (www.)?example.com/var1/var2/.../varn would bring the visitor directly to the correct page.

The contents of my .htaccess at the moment:

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]

The thing is, when losing the R flag in the last RewriteRule will bring me to the index.php file inside 'application' but it shows the home page instead of a login form when for example I go to example.com/site/login.

Which, I guess, the script does not see the vars. (if it did site/error would trigger) So it handles this as a request to example.com/application and not as example.com/application/var1/var2

I hope I did explain the problem better this time. Thanks for the great help 'till now.

4

3 回答 3

3

如果您只想将所有不存在的 / $var1 / $var2 重写/ application /尝试检查这些配置指令:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/

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


现在,如果您想将/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导致错误时删除该行。

实际上,我无法理解您的问题..您说:

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

现在,你对你的评论说:

所以任何到brainfeeder.be/application/$var1/$var2 的链接都应该显示为brainfeeder.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 ,如果您是程序员,请使用您的想法。您可以添加另一个条件,例如RewriteCond %{REQUEST_URI} !\.jpg$如果您不想使用.jpg之类的扩展名重定向文件,或者您想要这样:

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

请尝试使用以下代码更改.htaccess文件的来源:

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]
于 2013-05-08T08:21:58.597 回答
2

不知道你的问题是什么...

但这里有一个 .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
于 2013-05-08T07:06:21.453 回答
0

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

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

这应该是您需要做的所有事情,但是如果您需要更多详细信息,我会在此处更全面地描述该方法:

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

于 2014-04-20T17:59:45.180 回答