CakePHP .htaccess mod_rewrite configuration to exclude a particular folder/url

后端 未结 3 2017
半阙折子戏
半阙折子戏 2020-12-22 01:48

I have a CakePHP installation in a sub folder in my server, I want to install another application inside that subfolder:

root/public_html/subfolder/cake/
roo         


        
相关标签:
3条回答
  • 2020-12-22 01:59

    How to Exclude a Directory from CakePHP Routing:

    I ran into the same problem as you. I struggled, but finally found something that worked.
    (I'm replying because the answer above didn't work for me, but this did).

    My directory structure:

    root/public_html/app/
    root/public_html/lib/
    root/public_html/plugins/
    root/public_html/vendors/
    root/public_html/directory_to_exclude/
    

    What I added to CakePHP's default routing:

    RewriteCond %{REQUEST_URI} !^/directory_to_exclude/(.*)
    

    Effectively, I needed to allow people to access the subfolder separately (since that has a separate application). Also, in my case, I'm serving a CakePHP application by default from the root directory.
    So here's what my CakePHP 2.x htaccess looks like:

    Updated .Htaccess (this works for me)

    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^/directory_to_exclude/(.*)
       RewriteRule    ^$ app/webroot/    [L]
       RewriteRule    (.*) app/webroot/$1 [L]
    </IfModule>
    

    In your case, I understand you need this to work in a subfolder; here's my adjusted htaccess I didn't have a chance to test this, but I believe this (or something close) will work:

    The Final Product (for a subfolder)

    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^/subfolder/directory_to_exclude/(.*)
       RewriteRule    ^$ subfolder/app/webroot/    [L]
       RewriteRule    (.*) subfolder/app/webroot/$1 [L]
    </IfModule>
    
    0 讨论(0)
  • 2020-12-22 02:09

    Put this before the RewriteRule lines:

    RewriteCond %{REQUEST_URI} !^/my_custom_application
    
    0 讨论(0)
  • 2020-12-22 02:14

    You have to replace dir_to_exclude by your dir name.

    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^/(?:dir_to_exclude)(?:$|/)
       RewriteRule    ^$ app/webroot/    [L]
       RewriteCond %{REQUEST_URI} !^/(?:dir_to_exclude)(?:$|/)
       RewriteRule    (.*) app/webroot/$1 [L]
    </IfModule>
    

    If you want to have more than one excluded directory you have to add them separated by |additional_dir_to_exclude

    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^/(?:dir_to_exclude|additional_dir_to_exclude)(?:$|/)
       RewriteRule    ^$ app/webroot/    [L]
       RewriteCond %{REQUEST_URI} !^/(?:dir_to_exclude|additional_dir_to_exclude)(?:$|/)
       RewriteRule    (.*) app/webroot/$1 [L]
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题