Refresh a single part of a webpage without passing parameters - php

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.

Related

Wordpress Catch All URL

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.

$_GET from URL without file extension

I just can't figure out, how to make this work.
I have a main page into which I am including other pages, creating URL's without file extensions:
switch ($_GET['page'])
{
case "search":
include "pages/search.php";
break;
}
Then I have a GET form sending a value from the main page to the one included.
The URL looks like this: www.page.com/search?word=value but based on var_dump($_GET) there is no $_GET['word'].
From what you described it looks like you are using .htaccess to redirect the requests from /search to /index.php?page=search.
If that's the case, you are probably looking for [QSA] mod_rewrite flag. Read the documentation at https://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa.

redirect through .htaccess file

I have made one file say a.php. Now I want some thing like if one tries to open a.php then He should ne redirected to another page of same directory of site.
I want it throght .htaccess file.
I have written this code in my .htaccess file
RewriteEngine On
# This allows you to redirect index.html to a specific subfolder
Redirect /b.php /a.php
both pages are stored in same directory..
You could try
RewriteEngine On
RewriteRule /a\.php /otherfile.php [R=301,L]
Change the filenames were necessary.
If you could be more specific as to what you wish to achieve,perhaps I can supply you with a better solution.
Question that might help:
Is it only for the file a.php, or there are other request that will be handled the same way as this pattern?
Hope it helps!
Please refere to the link below. I think so it will help you out.
How do I redirect my site using a .htaccess file?
Thanks
Or, you can use this if your script has a request string.
Like you want to stay on the same page, but not to show a request string.
(in case you want to post or get some data through scripts)
RewriteEngine on
RewriteRule ^/path/to/a/([0-9]+) /path/to/a.php?foot=$1 [PT]
Also, you should consult with the link Codemaster Gabriel and Arvind Sridharan gave you.
you can use header function in order to redirect..
try this
inside a.php
type:-
<?php
header ('location: b.php'); //lets say b.php is the file you want to redirect
?>
it will work..
and it is recommended to use this instead of .htaccess due to security reason of apache

.htaccess redirect loop

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/");
}

How can I quickly set up a RESTful site using PHP without a Framework?

I would like to quickly set up a RESTful site using PHP without learning a PHP Framework. I would like to use a single .htaccess-file in Apache or a single rule using Nginx, so I easyli can change web server without changing my code.
So I want to direct all requests to a single PHP-file, and that file takes care of the RESTful-handling and call the right PHP-file.
In example:
The user request http://mysite.com/test
The server sends all requests to rest.php
The rest.php call test.php (maybe with a querystring).
If this can be done, is there a free PHP-script that works like my rest.php? or how can I do this PHP-script?
Using Apache Mod Rewrite:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^test$ rest.php [nc]
Yes, make a 404 Redirect to a page called rest.php. Use $url = $_SERVER['REQUEST_URI']; to examine the url. In rest.php you can redirect to wherever you want.
This only requires one rule (404 in your .htaccess file).
You have to be careful and make sure that if the page requested (e.g. mysite.com/test1 doesn't have a test1.php) errors out with a 404 you don't get yourself caught in a loop.
Modified slightly from the way that Drupal does it. This redirects everything to rest.php and puts the original requested URL into $_GET['q'] for you do take the appropriate action on. You can put this in the apache config, or in your .htaccess. Make sure mod_rewrite is enabled.
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ rest.php?q=$1 [L,QSA]
If all you then want to do is include the requested file, you can do something like this
<?php
if (!empty($_GET['q'])) {
$source = $_GET['q'] . '.php';
if (is_file($source)) {
include $source;
} else {
echo "Source missing.";
}
}
?>
You really don't want to do that, however; if someone were to request '/../../../etc/passwd', for example, you might end up serving up something you don't want to.
Something more like this is safer, I suppose.
<?php
if (!empty($_GET['q'])) {
$request = $_GET['q'];
switch ($request) {
case 'test1':
include 'test1.php';
break;
default:
echo 'Unrecognised request.';
break;
}
}
?>

Categories