开发者

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

开发者 https://www.devze.com 2022-12-20 11:00 出处:网络
I have a CakePHP installation in a sub folder in my server, I want to install another application inside that subfolder:

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

root/p开发者_JAVA百科ublic_html/subfolder/cake/
root/public_html/subfolder/app/

etc. now I need a custom application installed there:

root/public_html/subfolder/my_custom_application/

I have something like this in my .htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

How can I configure it to exclude the "my_custom_application" folder from that rules?

I tried this:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    ((?!my_custom_application).*) app/webroot/$1 [L]
</IfModule>

But I get this:

Error:  The requested address '/y_custom_application' was not found on this server.

Thanks in advance.

Mauricio.


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>


Put this before the RewriteRule lines:

RewriteCond %{REQUEST_URI} !^/my_custom_application


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

精彩评论

暂无评论...
验证码 换一张
取 消