Codeigniter redirect issue with https - .htaccess

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.

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.

Mod_rewrite for single.php

I'd like to use mod_rewrite to show pretty urls in my urls:
Instead of
.../juegos/plants-vs-zombies/?play=jugar
change to
.../juegos/plants-vs-zombies/jugar/
And
.../juegos/ddtank/?play=full
change to
.../juegos/ddtank/full/
I use the file "single.php" with this code:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parts = parse_url($url);
parse_str($parts['query'], $query);
$parametro = $query['play'];
if ($parametro == 'jugar')
{
include( get_template_directory() . '/single-play.php');
}
else if ($parametro == 'full')
{
include( get_template_directory() . '/single-full.php');
}
And in .htaccess I have this:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)play=jugar($|&)
RewriteRule ^(.*)/$ /jugar/?&%{QUERY_STRING}
But when I try to get the url with /jugar/ and /full/ at the end of the url, it displays an 404 error.
I don't know what else to do. I hope you can help me.
Sounds pretty straight forward to me:
RewriteEngine on
RewriteRule ^/?juegos/plants-vs-zombies/jugar/?$ /juegos/plants-vs-zombies/?play=1 [END]
For that to work the rewriting module has to be installed and enabled, obviously. The rule will work likewise in the http servers host configuration and in a dynamic configuration file (.htaccess). If you decide to use a dynamic configuration file, then you need to place that in the http hosts document root folder and enable its interpretation using the AllowOverride directive in the host configuration.
If you receive a http status 500 using the above rule ("server internal error") chances are that you operate a very old version of the apache http server. In that case try replacing the [END] flag with the [L] flag.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
what the browser sees /juegos/plants-vs-zombies/jugar/,
what the server sees ?play=juegos/plants-vs-zombies/jugar
after explode it changes to an Array ( [0] => juegos [1] => plants-vs-zombies/ [1] => jugar)
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?play=$1 [NC]
This to change$_GET['play'] to an array
$play = explode("/", $_GET['play']);
if(isset($_GET['play'])){
$play = explode("/", $_GET['play']);
//if you want to add `/` at the end
if(end($play) == ""){
array_pop($page);
}
}
Passing Query String Parameters in WordPress URL

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.

CakePHP: Can you globally modify all links to point to another directory?

My CakePHP app lives inside a subdirectory to keep it from crashing into a Wordpress installation that powers part of the website:
example.com/ <--root
/_wp <--Wordpress installed here
/page1
/page2
/_cake <--CakePHP installed here
/page3
/page4
To maintain consistency, I'm using mod_rewrite rules to rewrite URLs from example.com/_cake/pageX to example.com/pageX etc. This basically works, but CakePHP still creates links with /_cake/pageX. So if a user hovers over a link, the "_cake" will show up in the bottom of the browser and of course in the source code.
Is there a way to configure CakePHP to think it's actually in the site root so it creates the desired URLs for links etc?
I haven't found a way to configure CakePHP the way you want.
If you create your links with HtmlHelper::link or Router::url, you can add a member $url['base'] = false to the $url array argument. This prevents the _cake prefix being inserted in front of the URL.
As an alternative, you can create your own link() or url() function, which calls HtmlHelper::link and always adds base = false.
For this to work, you must also have a .htaccess in the document root, which rewrites the requests to their real destination
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /_cake/$0 [L]
You must also pay attention to requests destined for the _wp directory.

Yii and Mod Rewrite: redirect to user language translated url

I'm developing a multilanguage web app with Yii.
I applied changes to hide the index.php, changed urlFormat to path and added to the url path a slug with the user language example /it/index.php /en/index.php etc...
The problem now is that I need to redirect automatically to a different url once the user chooses another language. For example:
http://localhost/~antonio/project/it/women
needs to redirect to:
http://localhost/~antonio/project/it/femme
I have been playing with htaccess with no luck at all. Here is the actual code:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /~antonio/project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#My redirection code (tried a good few more to no use apart from this)
RewriteRule ^it/women$ it/femme
I would really appreciate any help on this issue, as it is driving me mad.
Thanks
Edit::
I surrendered with mod_rewrite. I found another solution by adding this code to /layout/main.php:
<?php
$onurl = Yii::app()->getRequest()->requestUri;
if ($onurl == "/~antonio/project/it/women") {
$this->redirect("/~antonio/project/it/femme");
} elseif ($onurl == "/~antonio/project/it/men") {
$this->redirect("/~antonio/project/it/uomme");
}
Rinse and repeat per combination of language/word
This might not work without setting up a proper Virtual Host (so that instead of local urls like http://localhost/~antonio/project/it/women you have nice urls like http://project1.dev). But I would do that anyway, since it makes your dev environment nicer! (Here's a place to start).
Anyway, I would try this: just leave the .htaccess file set like you normally would for "path" style urls, and then just parse a $_GET['lang'] parameter using the UrlManager? So you would have a urlManager setup like this in your config.php:
'urlManager'=>array(
'urlFormat'=>'path', // use path style parameters
'showScriptName'=>false, // get rid of index.php
'rules'=>array(
// this parses out the first chunk of url into the "lang" GET parameter
'<lang:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',
)
)
This way a url like this http://project1.dev/it/controller/action will redirect to the "action" action in your Controller like normal (controller/action), but in your action $_GET['lang'] will now have the value of "it".
I hope that helps, it's kind of a shot in the dark. I'm not sure how you are actually triggering the different languages, so a $_GET parameter might not be helpful after all.
Found a way to do this in htaccess:
RewriteCond %{REQUEST_URI} ^/~antonio/project/it/donna/shoes/(.*)$
RewriteRule ^it/donna/shoes/(.*)$ /~antonio/project/it/donna/calzature/$1 [L,R]

Resources