.htaccess rewrite rule for self made MVC application - php

I have created a small web application in PHP and I am struggling with the .htaccess file
I have this file structure:
/Main Project
//App
// controllers, models, core, views
//Public
// css
What i did till now is to redirect from localhost directly to the /public folder like this:
RewriteEngine On
RewriteRule ^$ /public/ [L]
How I can do it to work the same as localhost/public/controller/action/params but just localhost/controller/action/param1/param2 ?
Thank you in advance for trying to help me!

So after trying it this days i found a solution for me and i can redirect to the public folder and also pass the controllers, methods and parameters. If somebody will have the same example, here is what works for me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This is basically the Laravel example :)

Related

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.

Laravel 5 with cpanel redirect to default language URL

I have a Laravel 5.2 application on a cPanel hosting account in which I had to remove the "public" folder from the URL with the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Now I need to redirect everything incoming to just the main domain/page www.example.com to be redirected to www.example.com/en.
I tried some examples but all of them break the local css and img.
Reinventing the wheel is always a bad idea. What you should do is to setup your web server correctly by pointing it to a public directory and restarting it. Also, use original Laravel .htaccess.
After that use Laravel localization.
Then you could just copy Controller#method you using for /en route and put it into / route.

Why is my main controller not working in codeigniter

I am brand new to codeigniter but I have been a PHP developer for years.
A client of mine has a exciting codeigniter site that was developed my another developer and I'm trying to set up a dev server to work on the site.
I can load the index.php in views if i go to this url
http://102.236.250.217/app/views/nycity/
But none of the header/footers or css are working
I feel like my controller "class Nycity extends MY_Controller" is not working what could be the cause of this?
This is my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
Read codeigniter's user guide, learn directory structure, it's MVC. Find where your index.php file is and point domain there.
The rest is magic. Good luck!
P.s. MY_Controller should be located inside application/core according to the rules.
First, you have to check your route default controller, you should find routes.php in config folder, and this what you should write in:
$route['default_controller'] = 'your_default_controller';
Depending on your description, it should be like this
$route['default_controller'] = 'app';
And in your app controller should load the "nycity" view

Cakephp 3.0: rewrite .htaccess rule to allow new folder access?

I'm using cakephp 3.0 and I'm facing a problem. I upload my site on server on root level, where I place some demos which I can use for other purpose. Now issue is that when I access my site with domain name then it is working fine. But if I give any folder name which is placed on root level which is not the part of my site and is indiviual part of site. Then it is not accessible.
Below is my folder structure of root level
There a single folder named gallery is a demo. How can I allow this folder to access like this http://hostname.com/gallery or any other new folder.
My .htaccess file code is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Please help me on this issue which is more appreciable.
Thanks in advance.
Just move your "gallery" folder under "webroot".
Try this it's worked for me.
create the .htaccess file in "gallary" folder with following content.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>
and try with URL http://server_name.com/gallary
You have access for that folder and files.
Thanks.

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