How to remove public? from url in php project - .htaccess

I am currently working to build a small php mvc framework. in a framework i have a this folder structure.
-app
--controllers
-Post.php
-core
-logs
-public
--.htaccess
-- index.php
-vendor
in here index.php is working as Front Controller
in post controller is look like this..
<?php
/**
* Posts controller
*
*/
class Posts
{
public function index()
{
echo 'Hello index';
}
public function addNew()
{
echo 'Hello addNew';
}
}
in url, i want to remove project/public/?posts/index public/?. When i remove (public/?) and visit the url. its showing me this error message.
project/posts/index
The requested URL was not found on this server.
using public/? project/public/?posts/index is working fine. and its echo index message
project/public/
The .htaccess inside of the public folder contains:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
in project main root folder ...
i did't added .htaccess and index.php file.
in .htaccess when i add this line. url redirect to xammp welcome screen
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

I'd say you want to internally rewrite all incoming requests to the controller inside the /project/public folder. But that is not what you do. The rule you implemented (RewriteRule ^(.*)$ index.php?$1 [L,QSA]) only rewrites relative to the requested folder. No mentioning of "public" in there.
The actual setup you need depends a bit on your http host setup here. Where its DOCUMENT_ROOT points to. Most likely to the folder that contains the file system structure you posted in your question. If so you should implement a rule that rewrites all incoming requests to the /project/public folder.
Something like that, though you probably need to tweak it to match your actual setup:
RewriteEngine on
RewriteRule ^ /public/index.php?%{REQUEST_URI} [L,QSA]
You can implement such rule in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (if you have enabled those for the http host), so a ".htaccess" style file. That file should be located inside the folder your http hosts DOCUMENT_ROOT setting points to. So the folder containing the file system structure your posted.
Other setups are possible, this is just one option. The point is: you need to rewrite the requests to your controller. Where the controller actually is.

Related

.htaccess Redirecting url to a folder. Error on loading resources

I need to redirect from URL to a folder.
So, in the htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*) http://www.example.org/portal/$1 [P]
But, when the portal app is requested it calls many .css .js .png files...
When these files are requesteds, the URL works like this:
www.example.com/portal/file1.css
www.example.com/portal/file2.js...
and I receive the 404(file not found)
It because when the app requests the files, the htaccess intercepts the call and reirect it to www.example.com/portal/portal/file1.css
So, i need when the exactly domain www.example.com is called, the rule redirect to www.example.com/portal. But when some resource of portal be called, the redirect must be do to www.example.com/resource. Internally it working like: www.example.com/portal/portal and i receive the 404 error
Help-me please.
Thanks
Probably you're using a relative path to your assets href="/portal/file.css" and the root url is www.example.com/portal
Try using the links without directory like href="/file.css"

Silex: reset the root route when the app is not at the webroot level

I am playing with Silex, trying to use it as a RESTful json api at a shared web hosting. The host has Apache web server. I would like the Silex app to sit in the folder which I tentatively called experiments/api, so the app becomes at a different level than the webroot. As per the documentation, the .htaccess file that I placed in the Silex app folder looks like this:
RewriteEngine On
RewriteBase /experiments/api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ src/index.php [QSA,L]
(meaning that the app sits in the /experiments/api folder, and the main controller file is in src folder and is called index.php)
This gets the job done (i.e. the requests to /experiments/api/ are picked up by the Silex app), but the inconvenience is that the app now sees this /experiments/api/ prefix of the pathname.
For example. When I send a GET request to /experiments/api/hello I want the app to ignore the /experiments/api part, and to match only the /hello route. But currently the app tries to match this whole /experiments/api/hello path.
Is there a way to reset the root route for Silex to include the constant part of the path? I looked through the docs, but couldn’t find the answer.
You could use the mount feature.
Here's a quick and dirty example:
<?php
// when you define your controllers, instead of using the $app instance
// use an instance of a controllers_factory service
$app_routes = $app['controllers_factory'];
$app_routes->get('/', function(Application $app) {
return "this is the homepage";
})
->bind('home');
$app_routes->get('/somewhere/{someparameter}', function($someparameter) use ($app) {
return "this is /somewhere/" . $someparameter;
})
->bind('somewhere');
// notice the lack of / at the end of /experiments/api
$app->mount('/experiments/api', $app_routes);
//...

redirect single image from one directory to another

I have one website, say www.example.com. So when I access "http://www.example.com/program/resources/foo.gif" it serves me foo.gif image from /usr/share/roundcube/program/resources/foo.gif, I found that in access log.
So what I would like to do is, I would like to copy that image from /usr/share/roundcube/program/resources/ to my webroot /var/www/www.example.com/webroot/img/ and write rewrite rule so that when request comes for foo.gif, it should serve from /var/www/www.example.com/webroot/img not from /usr/share/roundcube/program/resources/.
I've tried this
RewriteEngine On
RewriteRule /program/resources/(.*) /img/$1 [L]
It's working fine. But what if I want to make a rule for single image that is foo.gif ?
You can make it more generic and simpler than that.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/override%{REQUEST_URI} -f
RewriteRule .* override/$0 [L,NS]
Now just put any file you don't want to come from RoundCube into the override folder, e.g. override/program/resources/foo.gif

Pretty URL via separate folder and index.html file inside or rule in .htaccess

I have static site (html + css) generated by nanoc.
It works under Apache.
I want use pretty url instead of .html suffix at the end. Like this:
http://domain.ru
http://domain.ru/page/page1/
http://domain.ru/page/page2/
instead of
http://domain.ru/index.html
http://domain.ru/page/page1.html
http://domain.ru/page/page2.html
So, the 1st way is to create the separate folders with names page1 and page2 and place inside index.html files with corresponding content. When client does request:
http://domain.ru/page/page1/ is loaded file page1/index.html
The 2nd way is to have such files structure:
page (folder)
- page1.html (file)
- page2.html (file)
and create rule for .htaccess file in root of the site:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)/$ /$1.html
So, the question is which way is better ? May be there are some additional costs of using
rewrite rules or pitfalls? May be there are some good practices for this questions for static generated sites.
PS:
In any way i use .htaccess file: i need switch on utf-8 encoding, enable 404 error handling , disallow browse folders
Thanks and sorry for bad english :)
I prefer to use index.html in directories, because it is server -independent. I don’t think there’s a good reason not to do it that way.

Getting Symfony2 to work on shared hosting using only .htaccess

I've been searching for this for 4 days now and couldn't find a working solution.
I want to make Symfony2 work on shared hosting without access to command line or httpd.conf (there's no way to set virtual host). All I can do, is just edit .htaccess files. In my web root directory I also have some other projects (like forum). The directory structure is:
public_html
|-forum
|-ox
'-Symfony
|-app
|-bin
<...>
I can make it work both in dev ant prod environments (routing works well), BUT it doesn't load any assets (js, css, images). In error log there's always the same:
request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /bundles/acmedemo/images/welcome-demo.gif" (uncaught exception)
Same happens if asset is loaded not from bundles, but also in twig as:
{{ asset('css/main.css') }}
Then it ends up with
request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /css/main.css" (uncaught exception)
My .htaccess in public_html is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# DEV ENVIRONMENT #
RewriteRule ^$ Symfony/web/app_dev.php [QSA]
RewriteRule ^(.*)$ Symfony/web/app_dev.php/$1 [QSA,L]
# PROD ENVIRONMENT #
#RewriteRule ^$ Symfony/web/app.php [QSA]
#RewriteRule ^(.*)$ Symfony/web/app.php/$1 [QSA,L]
Any suggestions how to make things right?
Interesting problem. After digging around the code I found following solution.
Create a class named PathPackage.php in src/Vendor/YourBundle/Templating/Asset folder with following code.
<?php
namespace Vendor\YourBundle\Templating\Asset;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;
class PathPackage extends BasePathPackage
{
/**
* Constructor.
*
* #param Request $request The current request
* #param string $version The version
* #param string $format The version format
*/
public function __construct(Request $request, $version = null, $format = null)
{
parent::__construct("/Symfony", $version, $format);
}
}
Then in your app/config/config.yml add the following parameter.
parameters:
// ...
templating.asset.path_package.class: Vendor\YourBundle\Templating\Asset\PathPackage
Now it will append /Symfony to the asset url parameter.
To summarize asset twig function calls getUrl method to determine the url. Which is extended by this class. Object of the class is passed as argument during templating.helper.assets service creation. Luckily PathPackage class is configurable. So solution was possible :).
Do php app/console assets:install ./web locally, and upload the content of web folder your remote shared hosting.

Resources