Exclude a root folder from CakePHP app - .htaccess

I am trying to exclude a first level directory from Cake (root folder app), as it should hold a different app.
/ holds the app
/development/ holds the tested version of the app
The directory structure:
Public folder
.htaccess [modified]
app
.htaccess [unmodified]
webroot
.htaccess [unmodified]
index.php [unmodified]
...
...
lib
Cake
...
development
app
webroot
index.php [dumps $_SERVER for test purposes]
So, my development structure still doesn't have an app inside (nor .htaccesses), just to test if my root .htaccess works.
This is my modification of the root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/development.*
RewriteRule (.*) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
What happens:
/development/ shows the apache index of development folder
/development/app/ shows the apache index of app folder
/development/app/webroot shows the root app (request is captured in spite of the development url match).
/development/app/webroot SHOULD show me my /development/app/webroot/index.php file, right?
What the hell am I missing here?

This turned out to be an oddball bug on my server.
The resulting .htaccess which works now is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
For reasons unknown to me, if I SSH'd to my user account and edited the .htaccess files through the command line, it wouldn't work!
When I uploaded the same files (via SFTP), it started working properly.
I am using cPanel (11.34.1) on CentOS (5.9).
Another example: if you wish to "ignore" multiple sub-folders on a CakePHP installation, you can use this pattern:
<IfModule mod_rewrite.c>
RewriteEngine on
# /development, /version1.5 and /version1.8 directories
# they all hold independent apps
# or completely non-cake-related content
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/.*)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Note that both RewriteCond instructions are identical.

Related

htaccess symfony2 redirect to web directory

I've just published my symfony proyect, but the url is http://url.com/web/. I want to change my .htaccess to point to web directory. So, the url I want it to be like http://url.com/. I've tried to change my .htaccess but I have not been successful.
my project is in a shared host and i can't create a virtual host. I used this .htaccess file in symfony root and works.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^/web/app_dev.php - [L]
RewriteRule ^/web/app.php - [L]
# Fix the bundles folder
RewriteRule ^bundles/(.*)$ /web/bundles/$1 [QSA,L]
# Fix the assets folder
RewriteRule ^assets/(.*)$ /web/assets/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /web/app.php [QSA,L]
RewriteRule ^(.*)$ /web/app_dev.php [QSA,L]
</IfModule>
https://undebugable.wordpress.com/2014/11/12/symfony2-htaccess-to-redirect-request-from-web-to/
Please read that page and look the Apache configurations.
http://symfony.com/doc/current/setup/web_server_configuration.html

How do you redirect all request to public/ folder in laravel 5

I have a classic Larevel 5 project structure and I need to redirect all requests to public/.
I am on a classic hosting environment so public/ is a subfolder of my document root.
I shall imagine it can be done via .htaccess but I still need to figure out how. Anyone can help?
Thanks
There are two solutions:
1. Using .htaccess with mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
2. You can add a index.php file containing the following code and put it under your root Laravel folder (public_html folder).
<?php
header('Location: public/');
You don't need to change anything in Laravel's default public/.htaccess file.
Just create a new .htaccess in the same level your public folder and add the following content to it:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
That simple!
This is an extract from another answer which may also help you.
--
Modify your public_html/.htaccess to redirect all requests to the public subfolder.
# public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect All Requests To The Subfolder
RewriteRule ^ /public
</IfModule>
Make sure you have the proper public_html/public/.htaccess (GitHub).
# public_html/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization}
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
If you use cPanel, then:
1.Go to folder:
/var/cpanel/userdata/my_domain
2.Edit the both domains:
my.domain and my.domain_SSL
Add to the documentroot section /public:
documentroot: /home/user/public_html/public
3.Rebuild Apache config:
/scripts/rebuildhttpdconf && service httpd restart
if you using apache , add .htaccess file to your root directory with this lines :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
and make sure your apache redirect and rewrite modules is working fine .
you can use vhost to redirect the all request to your public directory with set public as root of your project .
The ideal scenario is to have /home/user/public as a symlink from /home/user/laravel/public.

CakePHP [.htaccess], static files as the root of project

I would like to be able to access static files at the root of my project, in a folder at the same level of app/. It's because that is the only directory to wich I have the permission to read-write files on my server, so our images are uploaded there.
So if someone writes this URL:
www.mysite.com/img-rw
It displays images in the folder at [project-root]/my-rw-dir
Any ideas on how to edit .htaccess files for something like this to be done?
Thanks
Taking into account that this is a course project (as the OP said in comments) and you are NOT concerned about security flaws, here is what you could do.
By default CakePHP has 3 nested .htaccess files:
/.htaccess (Project root folder)
/app/.htaccess
/app/webroot/.htaccess
The first (topmost) of them is the first to respond when you try to access www.mysite.com:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Clearly, this sends the user request right into the /app/webroot folder. We could use the conditional check that exists in /app/webroot/.htacess to, instead of promptly redirecting to webroot, check if the given URL matches to a file or folder. In order to do so, you'll need to update the parent (/.htaccess) file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # Checks if the given URL matches to a folder
RewriteCond %{REQUEST_FILENAME} !-f # Checks if the given URL matches to a file
#RewriteRule ^$ app/webroot/ [L] # This line is commented, should be removed
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This way, the request will only be redirected to /app/webroot if none of the conditions are met. If you need only to return files, than you could remove RewriteCond %{REQUEST_FILENAME} !-d, which verifies if the URL matches to a folder ( -d = directory).
A cleaner version that would only check for files should look like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
With this tweak, accessing www.mysite.com/image.jpg should return the file image.jpg hosted at your project root (/image.jpg).
For anyone wondering, this works great :
.htaccess (project root)
<IfModule mod_rewrite.c>
RewriteEngine on
# This folder is a subfolder of project root but contains static images
# Accessed like so: www.mysite.com/imgrw/[file name]
RewriteRule ^imgrw/(.*)$ /dir-at-root-level/img/$1 [L]
# Everything else is handled by cakephp webroot
RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
RewriteRule ^(.*) app/webroot/$1 [L]
</IfModule>

Cakephp project not working on server

Currently working on a project and completed. Works fine in local. But when uploaded to server. It Doesnt work as it should be. CSS and JS and images are not linked. I checked all the .htaccess files in project folder and they seem fine. These are the codes in .htaccess files
in root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
in app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
in webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
These files were still same since uploaded to server. I also went through some articles and questions to make sure that am not messing with .htaccess files. Then i found this issue.
in local when i tried to open /localhost/project/app/Controller/UsersController.php in local it returns error as it should be
But when i tried the same in server
The page actually seems accessed. Since its controller no output is found but i can be sure the Controller is accessed directly. I cant figure out what kind of issue am going through. Am quite new to this. Any help will be appreciated.
Cake creates tmp files under tmp folder. You must need to give permission read/write to this folder. After making your code live please make 777 permission to your tmp folder and try to delete all those files under sub folders. It may work and please share your results. You will find tmp folder under app folder.
Your problem is in the in root .htaccess it should be like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ projectfolder/app/webroot/ [L]
RewriteRule (.*) projectfolder/app/webroot/$1 [L]
</IfModule>
The project works now(Have to contact support for enabling the mod_rewrite). I enabled
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
in config/core.php The problem is didnt have the mod_rewrite enabled in server. So enabling that in core supported the project to work without mod rewrite.
Thanks to
http://wwdj.wijndaele.com/getting-started-with-cakephp-without-mod_rewrite/
I had same problem, it was working on localhost but not on server, it was throwing 404 error.
It is because I had uploaded my website in subdirectory of sever root. I did many things to make it work but as I am using htaccess in root folder I found no solution on root level.
Finally I found a solution, edited existing htaccess of cakephp root folder and added "RewriteBase" after RewriteEngine on.
Full code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /name_of_the_subFolder
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Like if my project appear in 'xyz.com/one/two/cake/' then RewriteBase would be Rewritebase /one/two/cake/.
It worked for me.

Redirect url with .htaccess

I m sure that many people will say that this is duplicated but I try everything from other "Question"`s and nothings work for me.
The problem is that I move my project to the web server.
In this server I have folder "public_html" where I but my project Symfony.
Now to enter on my project I must write the following url: www.mydomain.com/Symfony/web/*
But I want to write a Rewrite Rule which will redirect from www.mydomain.com/Symfony/web/* to
www.mydomain.com/home/*.
To do this I try on 2 different ways with many combination of ReWrite rule.
In my public_html I create a .htaccess folder
I edit the .htaccess in Symfony/web folder
I add the following rule in both file but without success
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Symfony/web/(.*)$ www.mydomain.com/home/$1 [L,R=301]
Unfortunately without success. What I`m doing wrong?
My htaccess file look like
And all the time Error 404 Object not found
Symfony/web/.htaccess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteRule ^home/(.*)$ $1 [L,NC]
</IfModule>
It`s redirecting me but I receive again Object not found :(
I delete the .htaccess in public_html folder which is the root one for my server
public_html\.htaccess
RewriteEngine On
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
1: Place this code in /Symfony/web/.htaccess:
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [L]
2: Place this code in /public_html/.htaccess:
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
I'm going to go out on a limb and say that your rule is backwards. I think you want your URL to be www.mydomain.com/home/* in the browser... In which case the rule would be reversed. Also, your .htaccess should be in the root and you don't need to include the domain in the rewrite rule because you set a rewrite base.
RewriteRule ^home/(.*)$ Symfony/web/$1 [L,R=301]

Resources