I am trying/attempting to create my own MVC. I am really trying to manipulate the url. I am trying to get several parameters to form a full URL.
I want to turn
http://example.com/?action=account&user=JohnDoe
to
http://example.com/account/JohnDoe
But I cant seem to get the .htaccess file to work right :/
This is what I have
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?action=$1&user=$2 [NC,L]
When I go to http://example.com/account/JohnDoe I get a 404 error.
Your missing one parameter, try this
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?action=$1&user=$2 [NC,L]
Rather than set up a mod_rewrite rule for that specific URL, why not put an entire framework in place?
mod_rewrite as follows:
#URI PATH CONSTRUCTION
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?string=$1 [L,QSA]
</IfModule>
Then have your index.php file break up $_REQUEST['string'] into an array that you can test to direct the request to where it needs to go
if (isset($_GET['string'])) {
//DEALS WITH GET PARAMETERS
if (strstr($_GET['string'],"?") !== false) {
$pos = 0;
$tailstr = substr($_GET['string'],$pos);
$endpos = strpos($tailstr, "?");
$endpos = $endpos + strlen("?");
$string = substr($_GET['string'],$pos,$endpos);
} else {
$string = $_GET['string'];
}
$path = explode("/",$_GET['string']);
}
//Depth to 5 levels
for ($i=0;$i<5;$i++) {
if (!isset($path[$i])) $path[$i] = null;
}
global $path;
You now have a global array containing the individual elements in the URI
eg
http://example.com/account/JohnDoe
would give you:
$path[0] = 'account'
$path[1] = 'JohnDoe'
$path[2] = ''
$path[3] = ''
$path[4] = ''
Related
I am currently working on a website where pupils can create articles that can be commented by others.
I have a already developed a script that creates them and stores them in a mysql db.
Every one of these articles should be available under www.mydomain/articles/xyz/
but I don't want to create a new directory for everyone. So is it possible to pass the parameter article=xyz (www.maydomain/articles/articles.php&articles=xyz) to be shown as before mentioned?
I am sorry if my problem is too complex. If you have a question regarding it, do not hesitate to contact me! :)
Maybe you can do something like this with .htaccess file.
You can redirect every page to your index.phppage
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.php [L]
</IfModule>
And then you can handle your domain in your php file:
<?php
/*
Example URL
$url = www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
*/
$url = "www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3";
$url = explode("/querystring/", $url);
/*
Where $url[0] is (www.maydomain/articles),
and $url[1] is the rest of it (queryqtring/articles/xyz/param2/value2/param3/value3)
*/
// If you need you can include your page articles on your index page like this
$page_name = explode("/", $url[0]);
$page_name = end($page_name);
include("/path to your directory/". $page_name .".php");
$query_string = $url[1];
// And one more explode for query string:
$query_params = explode("/", $query_string);
for($i=0; $i<count($query_params); $i++)
{
// odd value is GET name
$key = $query_params[$i];
// even value is GET value
$value = (isset($query_params[$i+1])) ? $query_params[$i+1] : "";
$_GET[$key] = $value;
$i++;
}
echo "<pre>";
print_r($_GET);
echo "</pre>";
/*
// GET Output
Array
(
[articles] => xyz
[param2] => value2
[param3] => value3
)
*/
?>
it can be achieved using htaccess,See this Tutorial for htaccess, this will guide you
https://codex.wordpress.org/htaccess
https://www.sitepoint.com/htaccess-for-all/
I have a problem with this url and I want to know how to rewrite that into htaccess. I'm pretty new into this stuff so I'm curious on how to do this, for example this url:
WEBSITE/?page=online
and rewrite it to, for example:
WEBSITE/page/online
or this url:
WEBSITE/?page=account&hide=x
to, for example:
WEBSITE/page/account/hide/x
I use this PHP script to include the pages inside one HTML document:
<?php
if (empty($_GET))
{
include 'pages/index.php';
} else {
if (!file_exists('pages/' . $_GET['page'] . '.php' ))
{
header("Location: /");
} else {
include 'pages/' . $_GET['page'] . '.php';
}
}
?>
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match SITE/page/account/hide/x, etc
RewriteRule ^/?page/([^\.*]+)/([^\.*]+)/([^\.*]+)?$ index.php?page=$1&$2=$3 [L,QSA]
# Match /SITE/page/online etc.
RewriteRule ^/?page/([^\.*]+)/?$ index.php?page=$1 [L,QSA]
You could do something like this.
.htaccess file
RewriteEngine On
RewriteRule (.*) control.php [L]
This says take every request coming to your server and sends it into the control.php file. Then in control.php we process all the gets to directories. You'll probably want some fallbacks, e.g. no GET request; should it go to the root, 404, 403?
control.php
<?php
$parts = '';
foreach($_GET as $key => $directories) {
$parts .= $key . '/' . $directories . '/';
}
header('Location: ' . '/' . $parts);
?>
I have been struggling for months now, and I'm just not getting it. I'm trying to get clean urls with php on xampp, I either get server error 500 or www.something.com/root/index.php?page=whatever does not go away I want www.someting.com/page/queryresult/
Can anyone help?
I think the problem is linking the menu to the query and the sticky part is in the switch
function display_menus_new()
{
$sql = "SELECT * FROM menus";
$query = mysql_query($sql) or die(mysql_error());
$array = array();
if (mysql_num_rows($query)){
while($rows = mysql_fetch_array($query)){
$array[$rows['parent_id']][] = $rows;
}
loop_array($array);
}
}
function loop_array($array = array(), $parent_id = 0)
{
if(!empty($array[$parent_id])) {
echo '<ul>';
foreach($array[$parent_id] as $items){
echo '<li>';
switch($items['name']){
case 'Home': print_r('<a href="home.php" >'.($items['name']).'</a>');
$items = str_replace("name", "", "");
case 'About': print_r('<a href="about.php" >'.($items['name']).'</a>');
$items = str_replace("name", "", "");
case 'Services': print_r('<a href="services.php" >'.($items['name']).'</a>');
$items = str_replace("name", "", "");
case 'Contact': print_r('<a href="contact.php" >'.($items['name']).'</a>');
$items = str_replace("name", "", ""); }
//This part Connects the menu to the Database Query Where ?Page= the Data Value connection
print_r('<a href="?Page='.($items['name']).'" >');
echo $items['name'];
loop_array($array, $items['Cat']);
echo '</a></li>';
}
echo '</ul>';
}
}
i have xampp on windows;
.htaccess:
RewriteEngine On
# Run everything else but real files through parse.php
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !^/dev-boards
# RewriteCond %{REQUEST_URI} !^/tests
#RewriteCond %{HTTP_HOST} !^(admin|admintemplate)\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]
parse.php:
<?php
include("../connect.local.php");
session_start();
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
//http://localhost/viewprofile/0/yes
//$vars[0] is "viewprofile"
//$vars[1] is 0
//$vars[2] is "yes"
What i do is i use file_exists to use $vars[0] and $vars[1] to reference a folder/file. if the file doesnt exist, route to $vars[0]/index.php
if the folder doesnt exists, redirect to index.php
Hope this helps to get you started.
add a file called .htaccess in your home directory
start the rewrite engine at the top of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
In that file, you'll write some conditions for re-directing to a clean URL:
#first, we need to define the request, in this case, index.php?page=whatever
RewriteCond %{THE_REQUEST} /index.php?page=([0-9]*)
# now we need to make it look like it's just the clean url
RewriteRule ^$ /page/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+) /index.php?page=$1 [L]
This is a bit confusing, but basically what it means is that when your browser receives the ugly URL, it gets redirected to the pretty URL. Then, the re-write fills the pretty URL with the content from the ugly URL.
Close your if statement after you're done re-writing:
</IfModule>
I have actually tried the tutorial stated in here: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls
I have tried to use the PHP version of the Tutorial. However it doesn't seems to work. In fact it looks a little bit illogical for me.
This is the code which I'd need to place to the .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
As I have tested this actually redirects everything to the index.php file. So I have inserted this code to my index.php file:
$request = $_SERVER['REQUEST_URI'];
$params = split("/", $request);
$safe_pages = array("login");
if(in_array($params[0], $safe_pages)) {
echo 'ok'; //insert a file here
} else {
echo 'not ok'; //header to 403.php
}
This code is quite starightforward. If the URI is /login it should echo: "ok" (insert the file there).
However whenever I type: mywebsite.com/login it just always gives me the index.php, with the message: "not ok" so something should be wrong with the php code I guess.
Your are missing one more method on the .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This should suport url like 'page/item'
<?php
$request = $_SERVER['QUERY_STRING'];
$params = explode("/", $request);
$safe_pages = array("login");
if(in_array($params[0], $safe_pages)) {
echo 'ok'; //insert a file here
} else {
echo 'not ok'; //header to 403.php
}
?>
This dont work with url like 'page/item'
This should work:
<?php
$request = $_SERVER['REQUEST_URI'];
$params = explode("/", $request);
$safe_pages = array("login");
$last_item = count($params)-1;
if(in_array($params[$last_item], $safe_pages)) {
echo 'ok'; //insert a file here
} else {
echo 'not ok'; //header to 403.php
}
You are testing the wrong item. You should check for the string 'login' in the end of the array. And the function split is depreciated, use explode()
Hi i'm trying to send data over from a url rewrite page but unfortunately, its not working its just throwing me back to the main page or error page. The template & urlrewrite for rmbitter is what i'm trying to do. Currently domain.com/rmbitter.php loads perfectly file. I have a link on domain/boothwall.html and that link looks like this domain.com/rmbitter.php?ulnk=$usr&slnk=$lnk the rmbitter.tpl has the $_GET code. My problem I believe is with the rewrite its not allowing the variable to be passed over.
The reason for using the .tpl is because the page has a design layout that is needed. If i build a started rmbitter.php file with a test.html page and a link to rmbitter.php with the variable it works fine.
$inc = array(
'pictures' => 'icons.php',
'view_images' => 'templates/view_images.tpl',
'boothw' => 'templates/boothw.tpl',
'rmbitter' => 'templates/rmbitter.tpl'
);
//URL rewriting rules...
$rew = array(
'/view_images_public\/(.*)$/' => 'req=view_images&user=$1',
'/boothwall\.html$/' => 'req=boothw',
'/rmbitter\.php$/' => 'req=rmbitter'
);
url_rewrite.php
<?php
//get request
$url = $_SERVER['REQUEST_URI'];
if (strpos($url,'?PHPSESSID=')) $url = substr($url,0,strpos($url,'?PHPSESSID='));
while (strpos($url,'//') !== false) $url = str_replace('//','/',$url);
$url = substr($url,strlen(constant('dir')));
$url_array = explode('/', $url);
//make request string
$reqstr = '';
foreach ($url_array as $key => $value)
$reqstr .= '/'.$value;
$reqstr = substr($reqstr,1);
//other stuff
if (substr($reqstr,0,9) != 'index.php') {
$rewrite['/pages\/(.*)\.html$/'] = 'req=pages&id=$1';
$rewrite['/static\/(.*)\.html$/'] = 'req=static&id=$1';
$rewrite['/(.*)\.html$/'] = 'req=$1';
?>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
</IfModule>
I don't 100% understand what your problem is, but the general cure to all GET related mod_rewrite ailments is Query String Append (QSA).
RewriteRule ^(.*) index.php [L, QSA]
as the name says, it appends any incoming GET data to the new URL and passes it on to index.php.