I created a restful api using Silex, i want to serve it using apache2, i have different entry files that share the same API core, so multiple index.php are in different subfolders so that i can use different configurations.
Lets say i have two folders that i want to serve using 2 urls:
http://localhost/folder1/api/v1/endpointName
http://localhost/folder2/api/v1/endpointName
api/
folder1/index.php
folder2/index.php
I managed to write an .htaccess to serve each folder, however for some reason PHP doesn't get evaluated
Options -MultiViews
RewriteEngine On
RewriteBase /folder1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
I tried changing the php file to:
Test1
<?php
echo 'Test echo';exit;
require_once __DIR__ . '/../vendor/autoload.php';
$app = App\Rest::run('folder1_config');
However only Test1 is in the response. So the main problem is that PHP isn't evaluated and the other is that i understand that silex works properly with query strings starting with api/v1/..., so i want to tell apache2 using the .htaccess, to take as RewriteBase each folder and the rest of the query string api/v1/... gets passed to the php file.
I have tried different approaches with no luck.
Thanks.
Related
I've set up my VPS server and am trying to set up a Laravel project.
I have a /public_html/ folder with an index.html that's shown when visiting the website URL.
I have a laravel project in: /public_html/project_one/
The original index.php is located in /public_html/project_one/public/index.php
I want the website to show to Laravel project_one by default, but since yesterday I literally googled this for 4 hours, I tried every solution on stackoverflow, nothing worked... I think I've tried over 10 versions of .htaccess I could find.
The question:
Does anyone know how to setup the .htaccess inside /public_html/ so it redirects to my Laravel project?
I though I needed to upload my Laravel folder in: /public_html/project_one/ but is there a better location?
I want the main url to show the Laravel project. So www.mainurl.com is Laravel's project_one/public/index.php file. I do not want /project_one/ to be written in the URL!
Just out of curiosity, if I now visit: www.mainurl.com/project_one I get a 403 forbidden.
Failed solutions
In /public_html/ I tried to add .htaccess with:
DirectoryIndex index.php
#Redirect to /project_one/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/project_one/public
RewriteRule ^(/lucaphoto)?(.+) /project_one/public$2 [L]
#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/project_one/public
RewriteRule .? / [R=301,L]
This gives me something really strange:
On Chrome: it redirects me to www.mainurl.com/project_one -> And then gives me a 403 forbidden error.**
On safari: it redirects me nowhere but gives me this error:
Forbidden. You don't have permission to access / on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Laravel's original .htaccess File
By the way, Laravel has an original .htaccess file at /project_one/public/.htaccess. And it looks like this:
<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>
The actual laravel application ( anything but the public folder ) is not supposed to be public and therefore should not be placed inside the public ( public_html ) folder.
I actually tried severel things to get something similar to your setup running. My conclusion was - that only changing the .htaccess file will net e enough to run laravel inside a public folder.
The most easy solution to quickly setup a laravel application is to use a subdomain which directly points to the public folder.
www.mainurl.com /* points to .../public_html/ */
laravel.mainurl.com /* points to .../laravel/public/ OR ( but not good ) */
/* points to .../public_html/my-project/public/ */
What you actually will need to do then is to change the root of your domain / subdomain to point into another folder - no changes to .htaccess are needed then.
Update
If your site is www.example.com and you want your laravel app to show when opening this site you have two options.
Put the content of laravels public folder to public html and change the references to fit the public_html instead of public folder.
If your Filestructure on the server is like .../var/stuff/.../public_html/ then upload everything to ../var/stuff/my-project and then go to your hosters interface and make an internal redirect to the ../var/stuff/my-project/public folder and everything will be fine without chaning anything in your laravel code. ( anything but .env )
So the actual problem is that your domain points to the folder public_html, instead of the public folder of your app. However please dont put your app into public_html directly, put it parallell to it and change where the domain points to.
if you give us some information about your hoster we might can provide a guide
So, I have cakephp installed in what I believed to be the appropriate way for production code with actual cake files above the public_html folder. The app folder is removed from the cake folder and placed at the same level.
Loading the home page works fine with the databases and the debug working.
However, in trying to run some test files I get a 404. Obviously, I need some way to direct something like www.example.com/posts/index to the proper location. I am not sure as to why this is necessary since the index.php file tells cake where the app directory is located?
Also, if I need to use the .htaccess file, how do I point to the proper location and exactly what am I pointed at?
Update - I get the errors when I follow the blog tutorial and navigate to www.mywebsite.com/posts/index
If you have a production install the url example.com should point at app/webroot/, and the only .htaccess file you need is the default one from the webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Be sure to use the .htaccess file which corresponds to the exact version of CakePHP you are using as it has changed over the years.
Im trying to create a site where authorized users can download pdf files...
the pdf files are in the webroot auth is by php sessions...
so to protect files from hotlinking, i found a working solution
php:
at successful login write a new file named session id:
if($logged_in) {
touch('auths/' . session_id());
}
then the .htaccess checks if the session id file is exists, if file is not exists its redirects to login..
# checks if a file named
RewriteCond %{HTTP_COOKIE} PHPSESSID=(\w+)
RewriteCond %{DOCUMENT_ROOT}/auths/%1 -f
RewriteRule ^(.*)$ $1
RewriteRule /* /login.php [L]
This is working so far, did a simple testing environment....
The issue is this needs to be combined with a cakephp app which uses the following .htaccess:
# CakePHP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
So my question how could .htaccess modified so its only checking if the request is *.pdf and also works together with the cakephp rules...
Thank you !!
I don't think that combine .htaccess and CakePHP will do well. Both are different kind of software and don't have native communication method. Until you write your own module for Apache I don't think you'll make a good solution. On your place I'd rather read whole file to variable than print it with proper header. A memory costly solution but secure, server independent and pretty easy to modify in the future.
I want to use .htaccessto redirect different requests to the same folder.
E.g.:
domain.de/ordner1/fileX.html
domain.de/en/folder1/fileX.html
domain.de/it/casella1/fileX.html
So whenever something is requested out of /ordner1/, /folder1/ or /casella1/ I want .htaccess to fetch the requested file out of a specific directory like domain.de/all/fileX.html.
I want to prevent duplicate content but also keep the foldernames in the selected language.
Could you help me solve this problem?
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#skip css, js etc
RewriteCond %{REQUEST_URI} !\.(css|js)[NC]
#if request to ordner or folder1 or casella1, serve the file from all/
RewriteRule ^(ordner1|en/folder1|it/casella1)/(.+)$ all/$2 [L,NC]
In your docroot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(ordner1/|en/folder1/|it/casella1/)(.*$) all/$2 [L]
You would need to add extra names to map other translation equivalents.
I'm often updating my website through various design iterations, and want to simplify my life by putting each version in its own folder: ie: www.mysite.com/v1.
How can I store all the contents of my in that folder (/v1, /v2, etc) yet have it accessed by simply typing in www.mysite.com.
I don't want just want to redirect the url, I want to remove the v1 from the url entirely.
Assuming you're using Apache, you could use mod_rewrite for this. Simply create a .htaccess file in the root of your public directory with a simple rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) v2/$1 [L]
</IfModule>
This will rewrite all URL's to the v2 directory. If you update your site to a new version, simply change v2 into something else and all requests will be rewritten to that directory.