Htaccess - 如何获得'子文件夹'的价值。请看例子

时间:2012-11-20 13:05:18

标签: php .htaccess codeigniter

如果网站网址为www.domain.com/subfolder/sitecontent,那么如何更新下面的.htaccess文件,以便所有请求都转到/subfolder/index.php而不是/index.php。我知道我可以手动添加/subfolder/,但我需要它是动态的。有可能吗?

基本上我打算在同一台服务器上的2-3个不同的URL上使用相同的htaccess和同一组文件。例如一个是www.domain2.com/sitecontent(这里没有子文件夹)。所有这些URL都指向同一个站点。任何帮助将不胜感激。

这是我当前的.htaccess文件的样子。对于domain1,它位于/subfolder/.htaccess,对于第二个域示例,它位于/

<IfModule mod_rewrite.c>
    #http://codeigniter.com/wiki/mod_rewrite
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 /index.php
</IfModule>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

不知道是否抓住了你需要的东西,但我认为你只需要附上文件夹名称,可以试试这个:

<IfModule mod_rewrite.c>
    #http://codeigniter.com/wiki/mod_rewrite
    RewriteEngine On
    RewriteBase /subfolder

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /subfolder/index.php?/$1 [L]

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /subfolder/index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /subfolder/index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 /index.php
</IfModule>