I have been using the following code to redirect users to a domain for over 2 yrs.
Lets say a user will navigate to my-example.com the index page is a template that defaults to load a blog so the actual url is my-example.com/index.php?nid=blog however the the url to displaed in the browser is my-example.com/blog.
I've been using a .htaccess file with the below code to mask this url and make it more reader and SEO friendly: -
RewriteEngine On
RewriteRule ^blog(/)?$ /index.php?nid=blog [NC,L]
RewriteRule ^blog/([A-Za-z0-9-]+)(/)?$ /index.php?nid=blog&article=$1 [NC,L] # Handle product requests
In the index.php file I was using the below code to send users going to my-example to my-example.com/blog
if ($nid == "") {
header("Location: ./blog/");
}
This worked fine in php 4 (default for my host). However I need to use PHP 5 for my new site so have added AddType x-mapp-php5 .php to the top of the .htaccess so that Apache uses php 5 however this has nerfed my php header redirect.
Chrome and Firefox both give similar errors that the page isn't re-directing properly in a way that will never complete - a redirect loop.
I have not changed the php in my template only added AddType and an include for new content. Anyone any ideas?
I've tried using a .htaccess redirect to ./blog but this seems to cause the same error.
You probably had register_globals on earlier.
Read nid from $_GET["nid"] instead.
$nid = #$_GET["nid"];
if ($nid == "") {
header("Location: ./blog/");
}
Related
I'm developping a web application in PHP and I would like to refresh a single part of my application. I found some post which explains how to do that with some parameters passing into the URLlike follow:
index.php:
if ((isset($_GET['page'])) && (isset($authorizedPage[$_GET['page']]))) {
require_once ($authorizedPage[$_GET['page']]);
} else {
require_once ('./home.php');
}
So the URL http://example.com/index.php?page=login will display my login page without reloading my index.php... it's ok so far.
But my problem is more how to have the same behavior without passing parameter in the URL. Meaning that if I would like display the login page I will have the following URL http://example.com/login/
Could you help me please ?
For your information in the future, I will need to integrate the notion of multilingual website.
Thanks
I think that you want to change the way people load pages on your website, don't you? So that people can load pages like example.com/login/ and example.com/faq/ instead of example.com?page=login and example.com?page=faq.
You should use the mod_rewrite in a .htaccess file in the root of you server. See http://httpd.apache.org/docs/current/mod/mod_rewrite.html for documentation.
I use the code
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?0=$1&1=$2&2=$3&3=$4&4=$5&5=$6&6=$7&7=$8&8=$9 [L,QSA]
</IfModule>
in my .htaccess file to pass all the requests to my index.php file. Each folder in the url will be a GET parameter.
That's the first part of your problem. When you want to reload just a part of your webpage, use JQuery with AJAX, like Marek commented earlier.
The RewriteRule line in my code is a good pattern and will load the index.php file when someone is loading my website. So if someone loads http://mydomain.com/folder1/folder2/file, the file index.php?0=folder1&1=folder2&3=file&4=&5=&6=&7=&8=&9= will be loaded. Then in PHP you can switch on the $_GET parameters to load the right page:
<?php
switch ($_GET['0']) {
case 'faq':
{code}
break;
case 'login':
{code}
break;
default:
{code of you home page}
break;
}
?>
You can paste my .htaccess file in a blank notepad, save it as ".htaccess" and store it in the root of you server. Should work directly.
I'm trying to get a wordpress page to "run" another page based on the url. So if I have a main page like:
/extensions
That will run the regular extensions page. But if I have urls like:
/extensions/test
/extensions/test/again
/extensions/text/again/etc
They will all just run the extension (no "s") page:
/extension
The extension page will parse the url and do it's thing at that point. I tried setting up a redirect rule in the .htaccess file like so:
RewriteCond %{REQUEST_URI} ^/extensions/.*
RewriteRule . /extension [L]
But I can't seem to get it going. I'm assuming wordpress is always parsing via the index.php file, which is some how trumping my little rewrite possibly. Is there anyway to set this up on wordpress?
Found the answer here.
Note that this should go into the functions.php file and you will have to hit save in the permalinks settings EVERY time you make a change to the function to see the effects.
I've searched the site and got far enough where I've been successful at rewriting to a clean URL. Just need a bit more help.
I have a page with a record that I have successfully rewritten to a clean URL like so:
domain/record.php?id=1685 > to > domain/record-Gavin-Rees-1685 using the below:
.htaccess file:
RewriteRule ^record-(.*)-(.*)$ /record.php?id=$2 [L]
This in my php file:
$temp=str_replace(' ','-', $record [record_name]);
$temp=str_replace('.','', $temp);
<a href='/record-". $temp ."-".$record[id]." '>
This works perfect. The problem is.
I cannot get it to rewrite the other way so if you go directly to:
/record.php?id=1685 it still exists. i tried > RewriteRule ^record.php?id=$ /record-(.*)-(.*)$ [R,L]
This isn't possible in the .htacces rewrite rules, because you couldn't define a general rule, which knows about your software internals. If you want to do this in your .htaccess file, you have to create a rule for every single id which would result in a very large and unhandy .htaccess file.
The better way to get an redirection for direct script calls is, to do the redirect in the php file itself and set the http status codes(i.e. 301 - permanently moved).
The get the requested url, you could use $_SERVER['REQUEST_URI'] and check for /record.php at the beginning
I am trying to create a login page for some sites that I manage. So far it worked easy enough by having a login page which connects to a database which told the page where to redirect them.
I also have an .htaccess that rewrites the URL to pass through an authorisation page that just checks if a session has been started then pushes them through.
The problem I have is that when an admin logs in to the admin page it isn't parsing the php and when I go 'view source' I can see all the code... how do I get it to parse the php?
.htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ ../authorise.php?file=$1 [NC]
authorise.php
session_name('TSiteStats');
session_start();
if (isset($_SESSION['user_id'])) readfile ($_SESSION['redirect'] . '/' . $_REQUEST['file']);
else header("Location: ../login.php");
Just to be sure.
Does you file use the <?php and ?> tags. These tag will tell Apache to load the PHP code to be executed.
Also, does the script can be executed (has the authorization to be executed by Apache).
Last thing, your code is using readfile which output the content of the file and not execute this one. Are you sure you want to readfile and not include it?
Everything was working fine till I added my .htaccess file. What I'm trying to do is route all my users to their profile page. So www.darudude.com/user1 routes to www.darudude.com/userinfo.php?user=user1
My .htaccess file as this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]
However ever since I added this, it breaks my sessions. On every page I have a initialize a session, and the sessions stores the referrer. This is the piece of code that handles the important part.
if(isset($_SESSION['url'])){
$this->referrer = $_SESSION['url'];
}else{
$this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
and then I'm returned to the original page using this piece of code:
header("Location: ".$session->referrer);
So for example, without the .htaccess file, if I login through one of the pages everything works and I get redirected back to the page I logged in from (i.e. if I logged in from index.php I get redirected back to index.php; if faq.php, I get redirected back to faq.php). With the .htaccess file I keep getting sent to /userinfo.php leading me to thing its something wrong with my rewriterule
This is how its supposed to work:
index.php loads. the $_SESSION['url'] is set to index.php
a login form is enacted whos action redirects to process.php
process.php the $session->referrer is set from $_SESSION['url']
After the login is confirmed the page should redirect using: header("Location: ".$session->referrer);
This is how it worked originally without any problems.
However, after the .htaccess was created it seems to redirect me to userinfo.php. I think it has something to do with my rule.
Any ideas?
I'm not sure if I understand the problem, but the rewrite rule you're using seems to turn the request to /index.php into a request to /userinfo.php?user=/index.php which may not be what you want.
It's because you're relying on $_SERVER['PHP_SELF'], which does not include the query string (the ?user= part of the URI). It worked before because your pages didn't rely on query strings to uniquely identify themselves. You can use $_SERVER['REQUEST_URI'] instead, though look out for circumstances where you don't want the query string to be preserved and now it is being.
Incidentally, the \?* in your RewriteRule regex is doing exactly the same as thing as if it weren't there.
You could try logging in with AJAX, thus never having to refresh the page at all. A simple Google search will throw up plenty of results, see below. Even if you're not using jQuery (which alot of the tutorials seem to expect you to), it's still possible with basic Javascript, in fact that's how I wrote my AJAX log-in script before converting it to use jQuery later.
http://www.google.com/search?q=php+ajax+log-in