Slim Framework /public folder redirect - php

I am using Slim Framework v3. I've set up API and its working smoothly if I access http://localhost:8080/slimapp/public
I have default directory structure. My Sample API endpoint is http://localhost:8080/slimapp/public/cards which returns JSON response of my cards
How Could I change the public folder to the domain, So I would be able to access my cards endpoint with http://localhost:8080/slimapp/cards?

You can simply rewrite the requests with
RewriteCond %{REQUEST_URI} !^/slimapp/public
RewriteRule ^slimapp/(.*)$ /slimapp/public/$1 [L]
This will serve the appropriate public folder, without redirecting the client. The RewriteCond is needed to avoid a redirect loop.

You can also edit Slim's default .htaccess file as:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [QSA,L]
Note: .htaccess file must be located in server root

Related

Mod_rewrite rule for redirecting all requests within a subdirectory to the index of that subdirectory with path as variable

I have requests coming to my server already. I want to v2 out my API calls.
Currently I have the directory /v2/ and within that I have index.php
If I call /v2/index.php?path=users/123 I get the right data back from my database. All is well.
But to standardize my code, I want to redirect /v2/users/123 to /v2/index.php?path=users/123
I have mod_rewrite loaded in Apache, tested with phpinfo(). I am not sure whether I need to place my .htaccess within the v2 directory or at root. I do not have any .htaccess in root today, and I do not want to modify any calls not made to /v2/.
Any pointers where to place the .htaccess file and the right conditions to do so?
My current file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /v2/index.php?path=$1 [NC,L,QSA]
I have put it in the /v2/.htaccess directory, but navigating ahead of /v2 yields 404 errors.
Thanks!

htaccess - Redirect parent folder PHP script

I have this web api project which is developed by other company. The file structure is:
/project
---/app
---/ApiEndpoint.php
---/public
---/index.php
The DocumentRoot is pointing to /project/public. The index.php is working (http://myapi.com/), however when I try to browse into the api endpoint http://myapi.com/api/endpoint I got 404 error.
How do I configure the .htaccess to rewrite this condition?
/project/public/.htaccess config
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
The current configuration is there to make sure every request go to index.php (that is probably dispatching requests in some way) except for the files that actually are in the public directory (probably needed for static files like images, and such).
If you wrote a php yourself to be directly called by http://myapi.com/api/endpoint, you should put it in /project/app/public/api/endpoint/index.php.
BUT I suspect you should study that application more and understand the current dispatching method, before doing that.

htaccess for domain and subdomain with laravel

I'm trying to figure out how to set up a domain and subdomain to work on a shared hosting account. It is a Laravel 5.1 application.
My access file is
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
I just purchased another domain and added it on, but I get a 500 error. I renamed the access file and then it worked. So it has something to do with the access file. Essentially I want two separate domains with and I'm wanting two separate laravel applications, one for each.
I'm not familiar with atacceess.
Maybe, you get a redirect loop, because the rule isn't protected by a condition. Although the default htaccess of Laravel should already contain them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Olaf Dietsche's answer does work for me.
Here is another thing I came upon a website that also worked just before I saw his post. I guess I was reading that this would send a 404 to that directory.
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?main-topdomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-folder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
So along with this comes another question if anyone is in my boat. I have my subdirectory inside of my root directory.***
dlaugh.com/public_html
laravel folders and access**
inluding
app
bootstrap
config
database
etc...
but also I have my sub folder
app
bootstrap
config
database
etc...
**mysubdomain in that folder**
Is it better practice to put
-main_domain_folder and
-subdomain_folder
in public_html
and then the
/app
/config
/database
would be in the main_domain_folder rather than passing the subdomain through the main domain?

.htaccess configuration for Slim PHP REST API on a subdomain?

I currently have an api.php file built using the Slim Framework. I'd like to use something like api.mydomain.com (or mydomain.com/api) to reference the API file and make calls using http://api.mydomain.com as the base url. The Slim documentation calls for the following to be added to your .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
But if I'm correct, that returns the index.php file if the file requested doesn't exist. How best to setup a rewrite from api.mydomain.com or mydomain.com/api to the index.php? Everything we've found points to configuring this using .htaccess.
We used the following rewrite rule, but this resulted in having slashes ('/') appended to all of our URLs, which also required us to reconfigure our routes in Slim as well. (Example: "/messages" became "/messages/" and "/threads?id=123" became "/threads/?id=123")
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^api\.mydomain\.com [NC]
RewriteRule ^ api.php [QSA,L]
While it is possible to fix this with some complex apache rules (see the links included), this was actually addresses in Slim version 2.2+ so you are far better off just updating your version of slim (phar version set to "2." or "2.2.").
http://www.slimframework.com/install
If you decide to go the apache route (not recommended) then check these links:
https://webmasters.stackexchange.com/questions/23382/how-to-remove-trailing-slashes-from-url-with-htaccess
htaccess remove .php, .html and trailing slash

Symfony: Options for admin URL

In development mode my symfony admin/backend app can be accessed at http://localhost/backend_dev.php. For production mode, I created a controller php file, admin.php, so now in production the admin application can be accessed at http://www.domain.com/admin.php.
What do I have to do to allow the admin app to be accessed at domain.com/admin or admin.domain.com?
Thanks!
you can open new subdomain an on that subdomain (admin.domain.com) setup virtual host that points to server with your symfony app.
you can look at the full tutorial [here][1].
[1]: http://blog.mirthlab.com/2008/03/04/dynamically-loading-symfony-applications-via-subdomains/ here
You probably are better off putting everything admin like in the admin directory, but you can cheat by using mod_rewrite
RewriteRule ^admin/?$ admin.php [L]
Here are some basic ways you could do it:
Either dump admin.php into a folder called 'admin' in the root of www.domain.com, and rename admin.php to index.php. (Easiest solution)
Of course, this way you have to adjust all relative links in admin.php to one level up (appending '../' to the start of all relative urls should work), as well as all absolute links to reflect the changes.
Regarding your admin.domain.com, you should contact your webhost/domain name provider to setup a subdomain for you.
Or if your webhost allows .htaccess files, you could write a mod_rewrite rule.
i would create a module called admin...then in presumably the index action I would put whatever you had in your admin.php file.
then in your routing.yml file just point yourdomain.com/admin to the admin/index....that way you keep everything within the symfony front controller
Andrew
Make sure your DNS resolves the admin.domain.com correctly, then edit .htaccess in the /web to have mod_rewrite pick up on your subdomain and rewrite requests to admin.php. Optionally rename your admin.php to something less obvious or perhaps do a quick subdomain check inside it as well, or extend the rewrite with a 301 redirect if anyone hits domain.com/admin.php.
The following simple .htaccess works for me:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# The admin subdomain returns the backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^admin\.domain\..*
RewriteRule ^(.*)$ admin.php [QSA,L]
# Check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# No?, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Change domain to your own domain.

Categories