Handling url rewrites and static files with Kraken JS - node.js

I have a one page app which I'm converting from a more traditional Apache setup to KrakenJS. It all goes really well except for one bit.
The app uses sockets to change its state, and we used to generate friendly URLs for our users to get back to a previous search. Using a .htaccess setup like so :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|css|js)
RewriteRule ^(.*)$ index.php [L]
That way, our static files were handled properly, and all the rest was redirected to index.php which handled the parsing and went on to query the right results (eg : example.com/section1/category1, would redirect to the main page, and send a query through the socket for section1,category1 items.
I've tried modifying my main route in index.js to add a wildcard, but it fails to handle the static files (RewriteCond $1 !^(*static extensions*) )
router.get('/', function (req, res) {
...
}
This one works fine for the use case where the user starts at the frontpage, but (obviously) gives me non-existing routes if I want to access directly /sec1/cat1
router.get('*', function (req, res) {
...
}
This one redirects correctly, but cannot find the static files.
Any idea how to handle the static files in a similar fashion than the RewriteCond does?

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.

Yii 404 page - override parent htaccess

I'm using the Yii framework at my company site.
I have a frontend at domain.com/... and a Yii based app at domain.com/app/.. .
We recently added a 404 page to the frontend site, with htaccess's ErrorDocument handler at the frontend. Problem is, the Yii app inherits from that 404, which gives a bad page. How can I force it to use Yii's embedded error handling page?
Thanks in advance.
There are 2 good article talking bout this:
How to use default layout for error pages instead of errorXXX views
Handling errors using an action
The idea is, you create a action in out main Controller that will handle all page errors, so, you can change your htaccess to redirect your application to the Controller:
domain.com/site/error
Ended up using a RewriteCond redirection instead of ErrorDocument, resulting in a "rule" for the file to be redirected. Basically something like this:
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /404.php
Basically this means, "if file requested is not valid file, use this rewriterule". The first condition is to make sure it excludes the "app" folder, so it handles its own errors.

Resources