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?
Related
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.
I have a root directory which has only test index.php file and .htaccess. Can you please help with forwarding root request (example.com) to child directory (example.com/wp/index.php). I don't need any redirects, because they will change the URL in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule $^ /wp/index.php [L]
</IfModule>
It is my .htaccess file in root directory for now. And with that .htaccess, server just return me /index.php (root index, not from sub directory).
So I don't think that it is very hard question, but I can't find where I'm making mistakes.
RewriteRule $^ /wp/index.php [L]
You have the regex anchors (end of string $ and start of string ^) round the wrong way. However, you could also have issues if you have a DirectoryIndex set (mod_dir will trigger an internal subrequest for /index.php, so the URL-path is not necessarily empty).
Try something like the following instead:
RewriteEngine On
RewriteRule ^(index\.php)?$ /wp/index.php [L]
The RewriteRule pattern ^(index\.php)?$ matches either an empty URL-path, or index.php (the result of the mod_dir subrequest).
No need for the <IfModule> container and RewriteBase is not being used here.
There are several questions out there to that topic, but not a single one did match my question, because I am using Slim Framework 3.
One of my routes, for example, is /stats
When I make requests to http://localhost/slim-app/public/stats - it works.
But, I want to make requests to http://localhost/slim-app/stats - and that doesn't work. Slim gives me the error
Page Not Found The page you are looking for could not be found. Check
the address bar to ensure your URL is spelled correctly. If all else
fails, you can visit our home page at the link below.
My folder structure looks like that:
/slim-app
.htaccess
/logs
/public
index.php
.htaccess
/src
routes.php etc
/templates
/tests
/vendor
The .htaccess- File inside the slim-app folder looks like that:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
The .htaccess- File inside the public- folder like that:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
What should I change inside the htaccess- files to make it work? Thank you guys!
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!
I don't know such thing is possible or not practically, but let's give it a try. In past (more than three years ago), I came to know about solution like this (don't know exactly how it is), in one MVC tutorial.
Directory structure
WebRoot/
includes/
config/
public/
images/
js/
css/
index.php
init.php
What I Want To Do Is
When user access http://www.mysite.com/a/b/c/, redirect all requests to /public/index.php page. i.e., all requests to website will go through /public/index.php page, except images/, js/ and css/ directories, which are in public/.
Also, http://www.mysite.com/includes/ will redirect to index.php page, and get includes/ as $query.
Index.php page
<?php
// get init file from webroot directory, which will load all required php files.
define ('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . 'init.php');
// get query from url (here, $query = "a/b/c/")
if(isset($_GET['q'])) {
$query = $_GET['q'];
}
// insert images, css etc. resource
echo "<img src='http://www.mysite.com/images/foo.jpg' />";
echo "<script src='http://www.mysite.com/js/bar.js'></script>";
?>
This will be useful for them who are using web-hosting, which doesn't provide one step up directory access to webroot.
EDIT
OK, I found the site from where I got this concept. See two .htaccess files used in root directory and public directory. Link to MVC tutorial
I assume the .htaccess is in WebRoot.
To rewrite all requests, you just use RewriteRule. When you want to exclude some paths, you use one or more RewriteCond
RewriteEngine on
RewriteCond %{REQUEST_URI} !/images/
RewriteCond %{REQUEST_URI} !/css/
RewriteCond %{REQUEST_URI} !/js/
RewriteCond %{REQUEST_URI} !/public/index\.php
RewriteRule .* public/index.php?q=$0 [L,QSA]
RewriteRule ^images/.+ public/$0 [L]
RewriteRule ^js/.+ public/$0 [L]
RewriteRule ^css/.+ public/$0 [L]
The flag QSA appends any other existing query string as arguments to index.php. If you don't want to append any existing query string, just leave the QSA flag out.
If you want the rewrite to be visible to the client, you must add the R flag to the RewriteRule, i.e. [R,L,QSA] or [R,L].
You can use another tricks, where your page will not redirected to another page but you will restrict other user to see the directory index
Options -Indexes
Add this text in your .htaccess file, it will generate a 403 errror (forbidden)