I have a web site where I have to use a .htaccess file to redirect all requests to index.php, where redirecting is handled.
I want to rewrite the URL, and at the same time use HTTPS. Without HTTPS it works fine.
Code from working .htaccess without HTTPS. Browser gets this input: alert/create
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
This works fine, but without HTTPS. Browser URL becomes http://localhost/mypage/alert/create, and that's what I want.
I found a solution that allows me to use HTTPS:
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ https://%{SERVER_NAME}/mypage/index.php?controller=$1&action=$2&id=$3 [NC,L]
Page navigation works like a charm, but browser displays the URL like this:
https://localhost/mypage/index.php?controller=alert&action=create&id=
Requests are handled like this:
public function __construct($urlvalues) {
$this->urlvalues = $urlvalues;
if ($this->urlvalues['controller'] == "") {
$this->controller = "home";
} else {
$this->controller = $this->urlvalues['controller'];
}
if ($this->urlvalues['action'] == "") {
$this->action = "index";
} else {
$this->action = $this->urlvalues['action'];
}
}
I need some hints. I've been looking all over internet without solving my problem...
If I can use .htaccess, but implement HTTPS another way, that'll be perfect too.
Server code written in PHP, running on apache2.
I would have done this in two steps. The first that you already have. And this one :
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
Solved! Solution: Added this to .htaccess, in that order spesifically:
RewriteEngine on
#force https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Related
I need some help with URL rewriting.
I need just 1 dynamic page (signin) to be handled via SSL, all other pages need to be redirected to HTTP.
So here my .htaccess file for :443 virtualhost:
RewriteEngine on
RewriteRule ^signin$ https://www.page.com/?s=signin [L,NC]
RewriteCond %{REQUEST_URI} !^\/(signin)+ [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R=302,L]
What happens is, that https://www.page.com/signin gets redirected via the 302 redirect to http://www.page.com/?s=signin
What am I doing wrong?
Your question is tagged with PHP. Is your signin page PHP?
Add this to the top of your signin page and that page will force itself to the HTTPS.
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "") {
$redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location: $redirect");
}
Ok, I take this as an apache question. To explain what seems to happen first:
Your first rule redirects to
https://www.page.com/?s=signin
This does not match the REQUEST_URI Condition !^/(signin)+; so the second rule redirects to
http://www.page.com/?s=signin
Probably your condition should read something along the lines of:
RewriteCond %{REQUEST_URI} !^\/\?s=signin [NC]
To see what exactly mod_rewrite does, enable the rewrite log, if you can - how to do this depends on the apache version.
I have designed a Online shopping website. Everything works fine with http. Now I want that the profile and the payment gateway should use HTTPS instead of HTTP. I have not dealt with https before and so I am stuck here. Here is my code snippet. I want profile.php to use https. I have seen some post where it has been told to edit mod_rewrite or use some https redirect function. But how will I do that? I am using XAMPP.
if($rws['user_email']==$UserName && $rws['user_pass']==$UserPwd){
if($_REQUEST['remember']==1){
setcookie('uname',$UserName,time()+24*60*60);
setcookie('pwd',$UserPwd,time()+24*60*60);
}
$_SESSION['fname'] = $rws['user_fname'];
$_SESSION['ud'] = $rws['user_id'];
header('Location:profile.php');
}
You need to put a rewrite condition in your .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
This is the real url:
http://web/app/index.php?id=1
Here I have used current .htaccess and working fine with me.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
Now the url is : http://web.com/app/index/i and working fine
but here is the problem the real url
http://web.com/app/index.php?id=1&name=abc
How can I set mention url with .htaccess for keep it short or any other good solution for hiding URL-.
is it possible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.
If we say, good practice is to rewrite everything to one index page, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :
http://example.com/app -> $uri = /app
http://example.com/app/sub-app -> $uri = /app/sub-app
but also query string applies to the $uri variable:
http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
in this case, if you want to examine just uri part before ? question mark:
$realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
next you can examine and manipulate $uri;
Example:
$trimmedUri = trim($realRri, '/');
if ($trimmedUri == 'app')
{
// you can show app page
}
elseif ($trimmedUri == 'contact')
{
// show contact
}
else
{
// show 404 page not found
}
// you can have some dynamic also also
if (is_file($filePath = 'custom-page/'.$trimmedUri))
{
include $filePath;
}
else
{
// 404 page
}
Final Code (regarding your current script)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#rewrite php file
RewriteRule ^([a-zA-Z]+)$ $1.php
#rwwrite with id
RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
#rewrite with id and name
RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3
I think what you're looking for is something like AJAX, mostly because of
but it could go to other pages while the url stay one url static.
in which case, you would looking at JavaScript and not php. You would need a way to quickly deal with the data and change it how you want it. I've recently started learning Angular, and it shows a lot of promise. Go through the tutorial yourself (just google angular.js and the tutorial is on their website), or some other JavaScript library (unless you want to hard-code everything, in which case, good luck) like jQuery or MooTools (names mentioned because they're libraries i have knowledge with). To my knowledge (I am no code guru) but a website's URI is what tells the server what to show you. You can make shortcuts
If, however, you want for it to just not have the .php filename, then I believe that
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.php\??(.*)$ $1.php&$2
Should, theoretically get you what you want.
Note: I can't test it right now, so it may error out.
EDIT: It does error out.
I have just now upgraded one of my static website to a codeIgniter enabled website. I need to redirect the exact domain names of the old website to the new ones to avoid the 404 error.
Old pages > http://example.com/pagename.php
New pages > http://example.com/index.php/home/category_images/9(cat_id)
I do not a very high no. of pages so hard coding all the pages directly will not be a problem. I have tried :
RewriteEngine On
RewriteBase /
RewriteRule ^http://www.example.com/pagename.php$ http://example.com/index.php/home/category_images/9 [L,R=301]
Not working, I have no idea why. mod_rewrite is enable on my apache server.
Also, just for the confirmation please also tell me in which folder I should put this file, I am confused between root dir or Application folder.
Thank you.
Version 2 : Now after suggestion from Mr. Michael, I tried to implement it using the redirect function like this :
Default Controller function : home.php
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model('common_model');
$this->handle_redirects();
}
function handle_redirects ()
{
$redirects = Array(
'gorgeous-girls.php' => 'home/category_images/9'
);
$uri = implode('/', $this->uri->segments);
foreach ($redirects as $from => $to)
{
$from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
if (preg_match("#^{$from}$#i", $uri))
{
if (strpos($to, '$') !== false and strpos($from, '(') !== false)
$to = preg_replace("#^{$from}$#i", $to, $uri);
redirect($to , 'location', 301);
}
}
}
And my .htaccess looks like :
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
PS : I have removed index.php from my domain name structure. It looks like : www.example.com/controller/function/uri/uri
Now I am not getting an error page from my host, but I am getting an error page from the CodeIgniter.
Please help.
Considering that your Hypertext Access file should be routing everything through index.php anyway, it is best to handle your redirects using CodeIgniter's redirect() function.
An example is provided below, based on what I have in my Page controller, in my own framework extensions (it can be used anywhere where the redirect() function is available, as well as $this->uri->segments.
function handle_redirects () {
$redirects = Array(
'pagename.php' => 'home/category_images/9'
);
$uri = implode('/', $this->uri->segments);
foreach ($redirects as $from => $to)
{
$from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
if (preg_match("#^{$from}$#i", $uri))
{
if (strpos($to, '$') !== false and strpos($from, '(') !== false)
$to = preg_replace("#^{$from}$#i", $to, $uri);
redirect($to , 'location', 301);
}
}
}
If you were to pass a query to your pagename.php file in your framework, you could consider the following (note that I do not know what the intention of your pages are - this is a generic solution):
$redirects = Array(
'pagename.php?q=(:any)' => 'home/category_images/$1'
);
For example:
http://example.com/pagename.php?q=9
would redirect you to:
http://example.com/home/category_images/9
This solution works for me quite well, and is very efficient when it comes to cross-browser compatibility.
Note that your RewriteRules should look like this, or something similar to it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (xml|txt|css|js|jpg|png|gif)$ - [L]
RewriteRule .* index.php [L,QSA]
Edit: The above has been changed to accommodate for assets. Basically, it tells Apache to route everything through index.php, unless it is a file that exists AND has the extensions specified. (This is good practice for security purposes.)
Edit: Note that you should not include index.php in your URL as it is being stripped by the .htaccess file anyway.
Note that I use PHP 5.3 - though this solution should work for you. Please let me know if there are any problems.
You don't use the whole domain name in the match portion of the rewrite rule. Try this:
RewriteRule ^pagename.php$ /index.php/home/category_images/9 [L,R=301]
Of course you will likely need other rules for the server to understand that index.php is the file that is to be called, not something at /image.php/home/category_images/9
So perhaps a second rule like
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php/(.*) /index.php?q=$1 [L]
I just noticed that in your updated answer you are trying to redirect from www.example.com to example.com. If you do want to force one domain, you should probably add another rule in your .htaccess before either of the other rules mentioned like this:
RewriteCond %{HTTP_HOST} ^www.example.com$
Rewrite Rule ^(.*)$ http://example.com/$1 [L,R=301]
you can open you xampp/apache/conf/httpd.conf
After then check below line
#LoadModule rewrite_module modules/mod_rewrite.so
if Hash (#) exist in above the line. then remove it.
LoadModule rewrite_module modules/mod_rewrite.so
and restart your apache server and check you site.
After then site not working please let me know.
I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
You are able to link to users like this domain.com/username and that's nice.
Then I have different pages like
index.php?page=forum&id=1
index.php?page=someotherpage&id=1&anotherid=5
index.php?page=3rd
... and so on. I want them to look something like this:
domain.com/forum/23/title-of-the-thread
domain.com/page2/id1/id2
... and so on.
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
I was thinking about creating a file that checks the URL,
you actually have that file, it's index.php
if it matches any pages, and users and so on. Then it will redirect with a header location.
that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.
Just change your rule to more general one
RewriteRule ^(.*)$ index.php [L,QSA]
You basically have two options.
Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
Note every possible route (url rewrite) you have in the .htaccess
I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
you said
I was thinking about creating a file that checks the URL, if it
matches any pages, and users and so on. Then it will redirect with a
header location.
While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in
an extra HTTP roundtrip for the client, leading to slower page loads
the "pretty URL" won't stick, the browser will show the URL you've redirected to
losing link-juice (SEO)
This can be done entirely with htaccess or php
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2
read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html