overlapping htaccess commands? - php

the problem is the first rewriterule is for my referral values to be allowed with php extension hidden in the link.
eg. mylink.com/myref
status.php is a different page, however, if i input mylink.com/status it just goes directly to the index. i also want the php of status.php to be hidden as well.
what is the best solution for an overlapping rewrite rules?
the code :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?str=$1
RewriteRule ^status$ status.php [L]

I am sure after this my example you will get exact idea in detail about that... just copy & try to understand about this...
create one Project Folder, then Create one .htaccess file , user.php, index.php, test.php
then copy below code...
in .htaccess file
RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^test?$ test.php
RewriteRule ^user?$ user.php
RewriteRule ^user/([0-9]+)$ user.php?id=$1
RewriteRule ^game/(1)? http://triviamaker.com [R=301,L]
in user.php file
<?php
if(isset($_GET['id'])){
echo "User Id :".$_GET['id'];
}else{
echo "<h3>Opps write localhost/user/anynumber</h3>";
}
?>
in index.php file
<?php
echo "Hello World";
?>
in test.php file
<?php
echo $temp = $_GET['land'];
echo $temp = $_SERVER['QUERY_STRING'];
if($temp == "1" || $temp == "land=1"){
header('Location: https://example.com');
}else if($temp == "2" || $temp == "land=2"){
header('Location: https://gmail.com');
}else if($temp == "3" || $temp == "land=3"){
header('Location: https://apple.com');
}else if($temp == "4" || $temp == "land=4"){
header('Location: user/2');
}
?>
Now Run this files:
localhost/your_project_folder_name/
localhost/_your_project_folder_name/user
localhost/_your_project_folder_name/user.php
& also run with parameter like this
localhost/_your_project_folder_name/user/1
localhost/_your_project_folder_name/user/2
localhost/_your_project_folder_name/test?2
localhost/_your_project_folder_name/test?3
localhost/_your_project_folder_name/game/1
I hope with this example You can understand different different scenarios easily... Thank You

Related

how to create good htaccess

Excuse me , I just start to learn about .htaccess files . I'm trying to write it this way (my htaccess file)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /exmpl/vadik_route/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReqriteRule ^(.*)$ index.php/$l
</IfModule>
and my index.php
<?php
echo $_SERVER['PATH_INFO'];
?>
but server through this error.
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
How can I fix that
Change your .htaccess with this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
you can send the url by get method for example getme.php?url=/controller/model/
and you can take the url also divide by slash
$_GET['url'] //it is going to show you the url.
lets look at the .htaccess and explain it.
RewriteEngine On
RewriteBase / #dont forget to modify this part. it is explain which folder you project in
RewriteEngine On Options All -Indexes RewriteBase /directoryname/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
############### SEO ##########################
#http://www.example.com/hello/booboo/ it takes the url after .com/
RewriteRule ^(.*)$ getme.php?url=$1 [QSA,L]
now we sent all the url to getme.php and we can use it
getme.php
<?php
//we redirect to get in url=$1 so our get method name is url
$parca = explode("/", $_GET["url"]); //and we divided the url by slash .
echo $parca[0];//this is first part "/hello/
echo $parca[1];// and this is second part "/booboo/;
?>
<?php
session_start();
//Define Language file paths
define("LANG_EN_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/en/');
define("LANG_RU_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/ru/');
define("LANG_KR_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/kr/');
define("LANG_TR_PATH", $_SERVER['DOCUMENT_ROOT'] . '/exmpl/sayt/multilang/tr/');
if (isset($_GET['lang']))
{
// GET request found
if ($_GET['lang'] == 'ru')
{
include LANG_RU_PATH . 'ru.php';
$_SESSION['lang'] = 'ru';
}
else if ($_GET['lang']=='en') {
include LANG_EN_PATH .'en.php';
$_SESSION['lang'] = 'en';
}
else if ($_GET['lang']=='tr') {
include LANG_TR_PATH .'tr.php';
$_SESSION['lang'] = 'tr';
}
else
{
include LANG_KR_PATH . 'kr.php';
$_SESSION['lang'] = 'kr';
}
}
//translate for russian
else if (isset($_SESSION['lang']))
{
if ($_SESSION['lang'] == 'ru')
{
include LANG_RU_PATH . 'ru.php';
}
//translate for kyrg
else if($_SESSION['lang'] == 'en')
{
include LANG_KR_PATH . 'en.php';
$_SESSION['lang'] = 'en';
}
// translate for turkish
if($_SESSION['lang'] == 'tr')
{
include LANG_TR_PATH . 'tr.php';
$_SESSION['lang'] = 'tr';
}
else {
include LANG_KR_PATH . 'kr.php';
}
}
else
{
include LANG_KR_PATH . 'kr.php';
$_SESSION['lang'] = 'kr';
}
?>
could you write path for this , as clear url

Strange Forbidden 403 Error - Solution Needed

I have a blog with a add-post.php page which contains a simple form with the action:
<form id="form" action="add-post-php.php" method="POST" enctype="multipart/form-data">
I then have the file, add-post-php.php in the same folder as add-post.php. So I enter the details of my blog into the form and press submit and get:
Forbidden
You don't have permission to access /add-post-php.php on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
I've tested this on my localhost and it works correctly
The permission of the file is set at 0644, but I've also tried it at 0755, with no improvement.
There is nothing wrong with my .htaccess file and there are no other .htaccess files in the directory.
I've tried using the full url path in my form action
My add-post-php.php script in full is:
<?php
include("php/settings.php"); // Contains DB Connections
?>
<?php
$id= time();
$month = date("m");
$year = date("Y");
$path = "images/posts/$year/$month/";
$section = $_POST["section"];
$category = $_POST["category"];
$credit = $_POST["credit"];
$title = ucwords($_POST["title"]);
$text = $_POST["text"];
$exclusive = $_POST["exclusive"];
$added = date("Y-m-d H:i:s");
$photo = $_FILES["photo"]["name"];
$ext = substr(strrchr($photo, '.'), 1);
?>
<?php
$insert_sql = "INSERT INTO posts (id, section, category, credit, title, article, exclusive, added) VALUES('$id', '$section', '$category', '$credit', '$title', '$text', '$exclusive', '$added')";
$insert_res = mysqli_query($con, $insert_sql);
if(mysqli_affected_rows($con)>0){
move_uploaded_file($_FILES["photo"]["tmp_name"],"$path" . $id . "." . $ext);
}
else{
echo "0";
exit();
};
?>
<?php
header("Location: post.php?id=$id");
exit();
?>
Here is my .htaccess:
Options -MultiViews
DirectoryIndex posts.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^posts/([0-9]+)/?$ posts.php?currentpage=$1 [NC,L,QSA]
RewriteRule ^section/([\w-]+)/?$ section.php?section=$1 [NC,L,QSA]
RewriteRule ^section/([\w-]+)/([0-9]+)/?$ section.php?section=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^posts/([\w-]+)/?$ posts.php?category=$1 [NC,L,QSA]
RewriteRule ^posts/([\w-]+)/([0-9]+)/?$ posts.php?category=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^post/([0-9]+)/([\w-]+)/?$ post.php?id=$1&title=$2 [NC,L,QSA]
RewriteRule ^sites/([0-9]+)/?$ sites.php?currentpage=$1 [NC,L,QSA]
RewriteRule ^posts posts.php
RewriteRule ^section section.php
RewriteRule ^sites sites.php
RewriteRule ^about about.php
RewriteRule ^advertise advertise.php
RewriteRule ^subscribe subscribe.php
And folder structure:
I've checked the php error log and there's nothing suspicious there. Does anyone have any idea why I'm getting the Forbidden Error when the file clearly exists and it's permissions are correct?
I found that the problem was with the include statement within the add-post-php.php file:
<?php
include("php/settings.php"); // Contains DB Connections
?>
I took this out and changed it to:
<?php
//Database Connect
$con = mysqli_connect("localhost","username","password","database");
?>
The script worked as soon as I edited this line of code.

Url rewrite, htaccess and PHP how to do it

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);
?>

Clean URL with PHP on XAMPP

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>

Mod_Rewrite PHP issue

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()

Categories