How to access an ZF2 application without “public” in URL - php

I completed my project on ZF2, then uploaded it on shared hosting. I would like a folder for the zend app with all the files inside, and then in the document root I would like the files from the public folder to be visible, because I want my URIs to look like www.example.com (not www.example.com/public/).

What I have done in those cases with ZF1 app is to throw an app directory in the document root and with your server's .htaccess equivalent made it not serve files from there (Deny from all). Then stick your gateway script into the document root and update the paths found in there including APPLICATION_ROOT and the path to the autoloader.
Hope this helps.

You can move the files inside /public to your webroot. However, please be aware that config files below your webroot are a security risk.

This one work.
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]

Related

Run laravel project in a directory folder on shared hosting

My question is: How can i run a laravel application in a directory
(not in root) on a shared webhosting?
For some of my clients I run Laravel applications in the root folder of their selected shared webhosting and remove the /public part from the URL. To make this work i move the index.php to the root folder and change the information from the index.php to:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Now i know that this can cause security problems but I already have found solutions to that case for shared webhosters.
The .htaccess i use to make this change is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.eot|\.woff|\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1/$2 [L,NC]
But in this case i want the laravel project to be run in a directory called test.
Folder structure:
root (public_html)
|-- test (directory with laravel project)
Now I'm not sure if i can simply change the .htaccess as i'm not very skilled with .htaccess files. What changes do i have to make, to make this work?
Note my urls:
// this link will be changed to:
www.[text-placeholder].com/test/public/css/style.css
// Output above result now:
www.[text-placeholder].com/public/css/style.css
// But should be:
www.[text-placeholder].com/test/public/css/style.css
It's easy, just place the project 1 level above the public_html folder and rename the public folder itself to public_html.
P.S. This is maybe a better way, than to use .htaccess, because now you really know fore sure the files outside of public html are out of your webroot (not accessible by hackers) and with htaccess you can make a mistake and the files are exposed.

MVC public folder htaccess is not working

I recently moved my index.php (the file that handles routing) and CSS, JavaScript, and font assets to a public/ folder. I only want items in this public/ folder to be accessible for security purposes. I ran into this problem when I realized I could visit mysite.com/composer.lock and view the composer lock file with my old .htaccess and folder setup.
Here is what I want
If I visit mysite.com/car/create, I want it to actually point to mysite.com/public/index.php?path=car/create
If I link to an asset such as mysite.com/css/style.css, I want it to really point to mysite.com/public/css/style.css
Here is my folder structure
Here is my .htaccess which is not working at all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/index.php?path=$1 [L,QSA]
</IfModule>
How can I fix this? It just generates empty pages and I can still directly visit files in the root directory, etc.
Your existing directives specifically avoid rewriting requests for existing files, so it would still enable you to visit files in the root directory. It will also rewrite static resources to public/index.php?path=, which will presumably fail.
Try the following instead:
RewriteEngine On
# Stop processing if already in the /public directory
RewriteRule ^public/ - [L]
# Static resources if they exist
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule (.+) public/$1 [L]
# Route all other requests
RewriteRule (.*) public/index.php?path=$1 [L,QSA]

ZEND - How to make it works on a shared host?

I have to move a Zend website on a shared hosting which is using Pretty URL but css files doesn't load when params is added. Before moving, i want to test with server ip adress to be sure.
For exemple
-SERVER_IP/~username/en (works)
-SERVER_IP/~username/en/news (fails because it search in "en" folder)
I would like to redirect every css files from path /css/ to my real css folder which has many css files.
I want same thing for every path /images/ and /js/ and maybe others.
Can someone help me ?
thank you!
UPDATE 1
Does path relative change between domain.com and SERVER_IP/~username? When i set /css/style.css, it search for SERVER_IP/css/style.css instead of SERVER_IP/~username/css/style.css. If i can make it works for both, my problems will be gone. But I can't test with domain because i don't have any for the moment
UPDATE 2
I've modified my hosts file in C:\windows\system32\drivers\etc\ to simulate with a real domain. As i expect, relative path aren't working the same ways.
- SERVER_IP/~username/index.php <img src="/img/world.jpg" /> => SERVER_IP/img/world.jpg (BAD, note i've lose ~username)
- www.domain.com/index.php <img src="/img/world.jpg" /> => www.domain.com/img/world.jpg (perfect)
With that change, all my path are good now and no need to redirect anything. But another problem appears. Since i use www.domain.com, index.php is shown in the URL and I don't want this : www.domain.com/index.php/en
UPDATE 3
I've found this rule : RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L] . index.php is now remove but when i'm on www.domain.com/en, it looks like en is not in the request uri and all my links move to www.domain.com/en/en/...
I paste here my .htaccess content :
Options +FollowSymLinks
Options -Indexes
RewriteEngine on
RewriteBase /
DirectoryIndex index.php index.html
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
UPDATE 4
I got it ! I describe my entire solution below!
COMPLETE FINAL SOLUTION
I've found a solution, everything is working #1 now on a shared hosting. Here is my complete solution, I hope it will save a lot of time to somebody.
1- Complete Zend Architecture (D=Directory, F=File)
- public_html/
(D) application/
(D) data/
(D) library/
(D) docs/
(D) nbproject/
(D) public/
(F) index.php
...
(F) .htaccess
2- Remove the basic .htaccess file in "public/" folder and place this one in the root folder "public_html/" (or ../public)
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
END !!! It's all you have to do, nothing more, no tricky or dirty code! This .htaccess file solve the DOCUMENT_ROOT config problem in httpd.conf file for shared hosting.
IMPORTANT NOTE
If your domain is not forwarded at that moment (OR you want to test before moving host from a server to another (my case)) and you want to access to your website with that kind of URL http://SERVER_IP/~username/, it will not works. The best solution is to temporaly edit your hosts file in C:\windows\system32\drivers\etc\ to add a line like this :
SERVER_IP domain.com It works great!

Running a Zend Framework Project on a shared server

I'm trying to upload my ZF Project to shared hosting
On my XAMPP, ZF's index page is located (and I access my page) at http://localhost/ZFprojectname/public
On the shared hosting in the root directory I have installed Joomla.
I want to access my ZF in the manner of http://mywebsite.com/booking/
so in this case, when going to http://mywebsite.com/booking/ I should be accessing ZF's public folder (as far as I understand).
And, I'd like to put my ZFproject in public_html/somefolderName/
How would you do it?
Shared hosting do not support defining Document Root path so you can use .htaccess to forward the request to public folder instead.
Create a .htaccess file inside the booking directory with the following rule.
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]

Zendframework project shows xyz.com/public/ so how to remove public

My zendFramework application on server shows
xyz.com/public/login
i want it like
xyz.com/login
how to achieve this ? is it possible to do with .htaccess file?
This is my answer from another question, but seems to fit in this question too. In the case you already have a .htaccess file inside public, use the htaccess solution from below.
The problem seems to be due to the root not being routed to /public.
The proper way: You need to setup a vhost and point the root to the public directory.
Another Way: You need to redirect every request inside public directory. The .htaccess for this file would be
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
You have to create a virtual server Zend have a tutorial to do so.
If you are not able to create virtual server by any reason. You can put the public index.php file outside the public folder. If you choose this method you have to add extra security methods, choosing this method lead to direct access of files.
You can prevent those direct access by creating .htaccess in those directories with these lines-
<Files *>
Deny from all
</Files>

Categories