Getting Symfony2 to work on shared hosting using only .htaccess - .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.

Related

How to remove public? from url in php project

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.

Codeigniter redirect issue with https

I'm adding a pdf function to my project, but I have a strange issue with redirecting the function after it is completed.
initially, it works well on localhost and on a test host not using ssl certificate, but it is not working on the domain associated with ssl certificate I don't know the reason and no debugging info can guide me when I should start.
The working code with no ssl : http://idev-inc.com/lab/rwahl.com/invoice
You have to book hotel to get the link works with you otherwise it will rediect you to the homepage.
The same function and repo at this url : https://rwahl.com/ndmilestone/invoice
I looked for the most threads about redirection with ssl and I updated the
.htaccessto the following :
RewriteEngine on
# Enforce SSL https://www.
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#####RewriteRule ^(.*)$ /ndmilestone/index.php/$1 [L]
RewriteRule ^(.*)$ /ndmilestone/index.php?/$1 [L,QSA]
I'm using mpdf library to generate the pdf files , And this is the invoice_as_pdf function :
function invoice_as_pdf(){
$assumptiondata=Array();
$filename="invoice".time().rand(1,9);
// As PDF creation takes a bit of memory, we're saving the created file in
// /downloads/reports/
$sub_folder="downloads/reports/$filename.pdf";
$pdfFilePath = FCPATH."/".$sub_folder;
$finalurl=base_url().$sub_folder;
// pass data to the view
if (file_exists($pdfFilePath) == FALSE){
ini_set('memory_limit','32M'); // boost the memory limit if it's low ;)
$this->theme->view('Admin/modules/global/invoice', $this->data, $this);
$html = $this->load->view('Admin/modules/global/idev_invoice_print', $data, true); // render the view into HTML
$this->load->library('pdf');
$mpdf = $this->pdf->load();
$mpdf=new mPDF('utf-8');
// $pdf->useAdobeCJK = true;
$mpdf->charset_in='UTF-8';
// Add a footer for good measure ;)
$mpdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$mpdf->SetDirectionality('rtl');
$mpdf=new mPDF('ar','A4','','',32,25,27,25,16,13);
$mpdf->SetDirectionality('rtl');
$mpdf->mirrorMargins = true;
$mpdf->WriteHTML($html); // write the HTML into the PDF
$mpdf->Output($pdfFilePath, 'F'); // save to file because we can
}
$this->load->helper('url'); // it is auto-loaded but I'm testing to call here also
redirect($finalurl); //redirect to the new PDF
}
The pdf files are generated successfully also, actually I tried several scenarios but till now I couldn't get the reason for that.
Thanks in advance.

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);
//...

URL rewriting without knowing page title

I currently have links at my site like this:
http://www.domain.com/locations/locationProfile.php?locationID=22
I am hoping, for SEO purposes that I could change this to something like this:
http://www.domain.com/locations/southern-maryland
"Southern Maryland" is currently pulled from a mysql db as the location name for location ID 22. Is it even possible to get this into the URL when my site structure currently utilizes the less attractive first version?
You can't use htaccess to do this for you. It's possible to use a RewriteMap that you can define in your server config (can't define it in htaccess file), but it's far easier if you just do this in your locationProfile.php script.
You can detect if the request is made with the query string using the REQUEST_URI value:
if ( $_SERVER['REQUEST_URI'] == '/locations/locationProfile.php' &&
isset($_GET['locationID'])) {
// go into DB and extract the location name then redirect
header('Location: /locations/' . $location-name);
}
Then in your htaccess file in your document root):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^locations/([^/]+)$ /locations/locationProfile.php?locationName=$1 [L,QSA]
And finally, make your php script look for the locationName parameter and server the proper page based on that instead of the ID.

Htaccess alias redirect fake multilanguage url

I have a domain www.domain.com with this kind of url
www.domain.com/my-italian-page.html (already rewritten by others htaccess rules)
I'd like to create a fake multilanguage url like
www.domain.com/my-english-page.html
The user will see in the address bar the rewritter url www.domain.com/my-english-page.html but the content that I'd like to show is the original www.domain.com/my-italian-page.html .
I'm on a shared server so I can't use apache vhost rule so I have to find a solution via htaccess.
Someone could help me to find the right way?
Thanks
So you want english URLs pointing to italian content? Hope your php script that generates these rewrite rules does the translating. But you'd do this for each one of your pages:
RewriteRule ^/?english-page.html$ /italian-page.html [L]
for each one of your pages.
Generally you want to keep the code executed in the web server as small as possible, so having a rewrite rule for each page is not usually a good idea. I suggest to implement this the way most CMS work when SEO URLs are enabled:
Rewrite any url (mydomain/anytext.html [actually you should not use the .html extension either]) to a script (eg. mydomain.tld/translate.php)
Use content of $_SERVER['PATH_INFO'] (should contain anytext.html) to display the correct page
Set correct HTTP response code if page does not exist: http_response_code(...) (see end of this answer for a function on php5 below 5.4: PHP: How to send HTTP response code?)
Sample .htaccess (actually originally "stolen" and severely modified from a typo3 setup)
RewriteEngine On
# Uncomment and modify line below if your script is not in web-root
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) translate.php$1 [L]
Very basic pseudo-code-like (not tested, there may be syntax errors) example:
<?php
// using a database? you have to escape the string
$db = setup_db();
$page = db->escape_string(basename($_SERVER['PATH_INFO']));
$page = my_translate_query($page);
// no database? do something like this.
$trans = array( 'english' => 'italian', 'italian' => 'italian' );
$page = 'default-name-or-empty-string';
if(isset($_SERVER['PATH_INFO'])) {
if(isset($trans[basename($_SERVER['PATH_INFO'])])) {
$page = $trans[$trans[basename($_SERVER['PATH_INFO'])]];
}
else {
http_response_code(404);
exit();
}
}
// need to redirect to another script? use this (causes reload in browser)
header("Location: otherscript.php/$page");
// you could also include it (no reload), try something like this
$_SERVER['PATH_INFO'] = '/'.$page;
// you *may* have to modify other variables like $_SERVER['PHP_SELF']
// to point to the other script
include('otherscript.php');
?>
I saw in your answer that you have another script - dispatcher.php - which you seem reluctant to modify. I modified my response accordingly, but please keep in mind that by far the easiest way would be to just modify the existing script to handle any English paths itself.

Resources