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.
Related
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!
I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...
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
My web application url is something like http://www.example.com
Now i want that the end user will always see http://www.example.com in there
browser inseted of something like http://www.example.com/index or any thing after the / will not show to the end user
i.e http://www.example.com/abc.php?id='someid'
will display in the user browser as http://www.example.com
Thank You in advance and sorry for the bad english.....
There are several ways to do that. REST web service, URL shortening, changing the alias or creating an .htacces file.
An easy way to do that would be creating an .htaccess file in your root directory.
If you’re running Apache, you can do that by creating a redirect in your site’s .htaccess file. If you don’t already have one, just create a new file called “.htaccess” in your site’s web root.
Save this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
That rewrite rule basically says, “If a request is coming in that doesn’t map to an existing folder or file, pass it along to index.php instead.”
source http://buildwithcraft.com/help/remove-index.php
and check this htaccess remove index.php from url
I have a normal php script which I want to use in a CakePHP application (created by a previous developer). I have made a folder in my CakePHP webroot folder:
Httpdocs -> app -> webroot -> folderx
Is it possible to run a standalone script in this folder?
Also, my script takes in an id, for example www.domain.com/folderx/index.php?id=123 and draws the information (ex name of the product) from the DB. However, I wish to rewrite the URL to:
www.domain.com/folderx/name-of-the-product. Is this possible at all?
The webroot already has an .htaccess file which handles the CakePHP rewrites. Would I be able to use a new .htaccess file with the rewrite instructions in www.domain.com/folderx/ for my standalone script.
You need to modify the existing rewrite rules to not rewrite requests that include /folderx/ to CakePHP. If you need help with that and the mod_rewrite manual isn't enough, share your rewrite rules.
By default, your htaccess, in webroot, must contain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
In this case, all the folders and files in the webroot should open normally