Apache redirection to public subfolder - php

I am trying to implement a free MVC found on net. My project is stored in http://localhost/project/
The file and folders structure looks like:
/project/
public/ (here I have index.php plus css subfolder, js etc)
controllers/
views/
models/
File index.php (main one) is being kept in public folder.
Now, when I try to access via web: http://localhost/project/employees it works well and it loads employees controller... But when try to access http://localhost/project/employees/methodtest I get File not Found + my CSS files (stored in public/css/main.css) cannot be load and it show path: http://localhost/employees/methodtest/main.css.
I would like to be able to call http://localhost/employees/somemethod/someparams/etc but with my css folders/files still working.
Here is my redirection setup:
RewriteEngine on
RewriteBase /employees/
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
Is above clear enough in order to get some help? - Thanks!

Related

Simple PHP API gives error 500 in Cpanel

I followed this tutorial to make a simple API with PHP. It works perfect on my computer. I added some other php files to the source folder and I added a composer package (Firebase/JWT) to the project, so a vendor folder and a couple composer files were created. Now the project folder looks like this:
/
- /plublic
-- .htaccess
-- index.php // requires src/functions.php
- /src
-- connection.php
-- functions.php // requires other src/.php files and JWT from composer
-- panel.php
-- responses.php
- /vendor
-- /composer // and files
-- /firebase // and files
-- autoload.php
.htaccess
composer.json
composer.lock
I uploaded this to my cpanel hosting (at api/) using a subdomain (api.mydomain.com). I made the subdomain with cpanel and first set the root folder to api/public but it didn't work (500 error). Then I tried pointing the subdomain to api/ but that didn't work either. What can I do? I think the problem is in the .htaccess files.
#root .htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
#/public .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I don't know much about .htaccess or composer, so I didn't move anything of that. Thanks.
It's pretty obvious why it does not work. Look at your rules:
#root .htaccess
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
The first Rule in your .htaccess file rewrites any request of the root, e.g. www.example.com or api.example.com/ to a subfolder /public.
The second Rule complements this and rewrites any other request to the subdirectory /public, e.g. api.example.com/test to api.example.com/public/test
So, effectively, one of these two rules will always fire.
Since you have set the root of api.example.com already to /public it cannot find a /public/public subfolder; thus, you get an error 500.
To resolve this, you could try to remove both rules in the root .htaccess or you can set the root of /api not to the subfolder.
The public/.htaccess file has a typical single entry point configuration and should be fine.
Always use a fresh browser in incognito mode to test the changes.

Route's base path for subdirectories

I have the following structure:
/home/
alvaro/
public_html/
Code Playground/
SlimTest/
logs/
public/
src/
templates/
vendor/
...
http://example.com points to /home/alvaro/public_html, thus http://example.com/Code Playground/SlimTest points to /home/alvaro/public_html/Code Playground/SlimTest, directory on which I've placed this .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
In short, loading http://example.com/Code Playground/SlimTest in the browser executes my /home/alvaro/public_html/Code Playground/SlimTest/public/index.php Slim/3.9.0 (slim/slim-skeleton) entry point.
My problem is that I can't get routes right. I've tried every conceivable combination:
$app->get('/Code Playground/SlimTest', …);
$app->get('/Code Playground/SlimTest/', …);
$app->get('/Code Playground/SlimTest/public', …);
$app->get('/', …);
// …
... but I always get the 404 Not Found page delivered by Slim's NotFound class.
Is there a way to get base path upon route definition so I can prepend it in get()?
If not, what would a hard-coded path look like?
Should I look for a fix in Apache, such as Alias?

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

htaccess and php app navigation

I'm getting crazy with a little problem on my simple web app. I made a simple calendar app with PHP, following MVC pattern.
Dirs structure is like:
calendar (root)
-- app/
-- controllers/
-- core/
-- models/
-- views/
init.php
-- public/
-- css/
-- scripts/
-- others dirs...
-- .htaccess
-- index.php
Every request are made like:
/calendar/public/[controller_name]/[controller_method]/[extra_params]
eg: calendar/public/home/all_events: call all_events method of Home class and then display with right view.
In each view.php I made link/button/etc with classic anchor, using href value like:
...
ex. href="booking/room" in order to call the controller for booking and specify what (room).
Everything seems works fine, but only locally.
When I try to put it on server all links don't work, causing a 404 page not found.
I'm trying to put it into an existing website made with Joomla. every request goes to a specific directory called sitename.com/, where there are all Joomla files plus my directory with the app.
There, I can just reach the app home page (default) but every link is not working.
I tried everything, but nothing seems to work.
I think the issue is in Apache configuration (or .htaccess). It die with a 404 page not found error and my index.php is never called.
This is my htaccess:
Options -MultiViews
RewriteEngine on
RewriteBase /sitename.com/calendar/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Or maybe I can't link pages with in that way, but how can I call specific controller/method from html?

CodeIgniter Noob - How does the index.php file work?

Here is my views directory:
- views/
- home.php
- contact.php
- assets/
- css/
- main.css
config.php: (changing this doesn't seem to do anything)
$config['index_page'] = 'index.php';
routes.php:
$route['default_controller'] = "welcome";
When I request www.example.com/index.php, the default page (i.e. home.php) loads and all of the assets work. However, when I request www.example.com/index.php/welcome or www.example.com/index.php/welcome/index, the page loads but the assets don't work. The same also happens if I try to load the page from a link from the home page.
I have no idea what index.php is for. I want to be able to just request www.example.com/welcome/index which will call the welcome.php controller and call the index method. application/index.php looks pretty important for everything to work, do I haven't deleted it, but I don't really want it. What can I do?
Thanks.
I think you're asking 3 different questions here.
Firstly index.php is the main entry point for your codeigniter app. It's very important as all of your routes will go through the index. The reason you can change it in the config is so you can rename it to something other than index.php if your setup requires it
Secondly, just guessing here but I think your assets are being loaded using a relative path; prefix all of your assets with base_url(); e.g
<?=base_url();?>assets/css/style.css
Thirdly, you'll need a htaccess file to hide the index.php (to give you www.example.com/welcome), or the equivalent if you're not using an apache server, which will look like the below (taken from http://www.codeigniter.com/user_guide/general/urls.html:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Enable mode Rewriting on your server LAMP/WAMP and add a .htaccess file with following code
RewriteEngine on
RewriteCond $1 !^(index.php|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
after doing above process you can use your urls without index.php i.e like www.example.com/welcome/index as well as your assets directory www.example.com/assets
Before all learn How MVC framework will work,
www.example.com/controller/function
If you call www.example.com , then default controller will load. You can change this in config.php
Inside the function you can call view ( your actual html viewable scripts ). follow the user guide from https://ellislab.com/codeigniter/user-guide
by default codeignitor url will be like www.example.com/index.php/controller/function
index.php can be removed using .htaccess

Categories