Configuration .htaccess in SLIM Framework - php

How do I adjust the .htaccess file for my slim-framework?
I have this folders/files:
/src/slim.php
/public/.htaccess
My URL points to the "public" directory. And I want to create a virtual "folder" so if I call http://example.com/api/... it should open my slim application in the src folder.
I have created this .htaccess file, but it doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api ../src/slim.php
This error message appears:

My URL points to the "public" directory.
:
The document root is "htdocs/project/src".
It's not clear how your "URL points to the public directory", when your document root appears to point elsewhere? Do you have another .htaccess file in the document root perhaps?
Anyway, it looks like you might be trying to rewrite to a file that is outside the document root? That is not possible using mod_rewrite in a .htaccess context. The server will likely respond with a 400 Bad Request.

Related

htaccess in laravel application

I've setup a laravel application on a shared server, the direct url works fine but when I access the other pages I receive the error message: The requested URL /contact was not found on this server.
but if I then type "http://unit13productions.co.uk/index.php/contact" this shows the correct page.
I'm not sure if this is the htaccess file or the index.php file?
My file directories on the server are as follow:
unit13 folder:
public_html folder:
my file directories inside my index.php is:
autoloader:
require __DIR__.'/../unit13/vendor/autoload.php';
turn the lights on:
$app = require_once __DIR__.'/../unit13/bootstrap/app.php';
contents of my htaccess file:
AddType x-httpd-php71 .php
This sounds like it could be an issue with your .htaccess file more than anything else. Try renaming your .htaccess file to .htaccess1 just so it isn't used and then create a new one and add the following to it
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The details on how to configure your application for either Apache or Nginx is listed on the laravel documentation which I've listed below.
https://laravel.com/docs/5.7/installation#pretty-urls
Hope this helps!

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.

Slim Framework .htaccess HTTPS

I am using Slim for an API which is working really well under http but when I request the URL in https I get a 404 error.
I think it has something to do with my .htaccess file, but I am not sure. Can anyone point out what am I doing wrong here.
.htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Depending on your server configuration the server might serve another directory when accessed via httpd ( eg httpsdocs instead of httpdocs).
You'll either have to change the configuration, copy the content or try to create a symlink from the http directory to the https directory.

.htaccess for site in sub-directory using Yii framework

I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.

files inside root directory not accessible mod_rewrite PHP

The file structure looks like this
root
root/x
root/x/y.php
root/index.php
.htaccess code
RewriteRule (.*) /x/$1 [L]
What the htaccess file does is to remove the folder name (x) from the url so accessing y.php = http://localhost/y.php instead of http://localhost/x/y.php . This works but my problem now is the index.php shows something like this:
Index of /x
Parent Directory
y.php
I can't access the index.php. I believe the x became the root folder.
Thanks for your help!
First, you shouldn't allow index displays like this on a public server ... on your localhost it may be okay, but still, you can disable it in your .htaccess by adding this:
Options -Indexes
To address your question, you should probably add the following conditions before your rewrite rule so that the rule won't apply to any files or directories that actually exist on disk:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This won't fix the issue you have because the .htaccess is redirecting to a non-existant /x/index.php file ... this is why it's showing the index listing for the /x directory

Categories