How can i do the below probably .htaccess ?
/sp/sitea => /check.php?page=sitea #no trailing forward slash
/sp/sitea/ => /check.php?page=sitea #trailing forward slash
/sp/sitea/index.php => /check.php?page=sitea/index.php #includes a file
/sp/siteb => /check.php?page=siteb
/sp/index.php , /sp/login.php => no redirect
I then want to check the DB using PHP for sitea to see if the user is logged in and then redirect to /sp/sitea If they are not logged in for that site redirect to /login.php
I tried .htaccess below , in sp folder, but doesnt redirect, im not really .htaccess master
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^\/(.*)$ sp/check.php?path=$1 [L]
I solved this with a modified .htaccess file
RewriteEngine on
RewriteOptions MaxRedirects=2
RewriteBase /
#dont check non pages
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule ^(.*)/(.*)$ /sp/check.php?path=$1&path2=$2 [L,P] #P keeps address in bar after redirect
check.php
<?php
//htaccess redirects here for sitepreview subdomains, then do authentication check and output the html
//cant redirect to page or get redirect loop
session_start();
$dir = $_GET["path"];
$dir2 = $_GET["path2"];
//echo $dir;
if(!isset($_SESSION['login_user'])){
header("Location: /sp/index.php",true,301);
exit();
}else {
$name = $_SESSION['login_user']["name"];
if($name == $dir) {
$homepage = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/sp/$dir/" . (isset($dir2) && strpos($dir2, ".htm") ? $dir2 : "index.html") );
echo $homepage;
}
else {
header("Location: /sp/index.php",true,301);
exit();
}
exit();
}
Related
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
Having problem in mod-rewrite .HTACCESS and php.
I have a page name users.php this page display selected user and my normal link is like this users.php?member=John then i changed it to clean url like this users/member/John but my problem is when you visit the page using normal url it will work fine but now i have rewrite it in my .HTACCESS it will take me to user page but not information will display it will be empty
Here is my .HTACCESS
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+users\.php\?member=([^\s&]+) [NC]
RewriteRule ^ users/member/%1? [R=301,L]
RewriteRule ^users/member/([^/]+)/?$ users.php?member=$1 [L,QSA]
HERE IS MY PHP CODE
<?php
if(isset($_GET['member'])) {
include($root . '_inc/dbconn.php');
try{
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmtData = $db_conn->prepare("SELECT * FROM users WHERE username=:getmember ");
$stmtData->bindParam(":getmember", $_GET['member']);
$stmtData->execute();
$UserData = $stmtData->fetch(PDO::FETCH_OBJ);
if ($UserData) {
$Dusername = $UserData->username;
$Dphoto = $UserData->photo;
$Demail = $UserData->email;
}
}
catch(PDOException $e)
{
echo "Error:" . $e->getMessage();
}
$db_conn = null;
}
?>
<?php echo $Dusername;?>
The problem is your RewriteCond. You have to negate the condition (note the '!' sign):
RewriteCond %{REQUEST_URI} !^[A-Z]{3,}\s/+users\.php\?member=([^\s&]+) [NC]
RewriteRule ^users/member/%1? [R=301,L]
RewriteRule ^users/member/([^/]+)/?$ users.php?member=$1 [L,QSA]
You can test your Rewrite Rules on online htaccess tester.
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
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 am trying to log to data base all URL that user type.
After this if the path exists I want to redirect the user to path
Else I want to leave the user in the root directory.
The URL that I type http://www.mydomain.com/folder1/index.php
The output screen
folder1/index.php
Array ( [0] => folder1 [1] => index.php ) url = to folder 1
folder1/index.php
The root directory index.php
<?php
if ($_GET['url']!= "") {
// url not empty there is query string
$url = $_GET['url'];
$url = rtrim($url, '/');
echo ($url.'<br />');
$url = explode('/', $url);
$file = $url[0];
print_r($url);
}
else // url empty it is root directory
{echo ('<br /> url empty <br />');}
if ($url[0] == 'folder1'){
echo ('url = to folder 1<br />');
$path = $url[0].'/index.php';
//the path exists do redirect to folder1/index.php
echo $path ;
header( 'Location: http://www.mydomain.com/{$path}' ) ;
}
?>
The Htaccess
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.mydomain\.com
RewriteRule (.*) //www.mydomain.com/$1 [R=301,L]
RewriteRule \.(sql)$ - [F]
RewriteRule \.(txt)$ - [F]
RewriteRule \.(zip)$ - [F]
RewriteRule \.(rar)$ - [F]
<IfModule mod_rewrite.c>
# For security reasons, Option followsymlinks cannot be overridden.
# Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
Options -Indexes
RewriteEngine On
RewriteCond %{http_host} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,NC]
RewriteRule ^([^\.]+)$ index.php?url=$1 [QSA,L,NE]
</IfModule>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NE]
Many thanks
I think its the quote problem change with double quotes
header( "Location: http://www.mydomain.com/{$path}" ) ;
Remove the echo before this line and write exit(); after that
header( "Location: http://www.mydomain.com/{$path}" );
exit();
You cannot have any output before any header line, so you have to remove all echoes before the header location sentence:
<?php
if ($_GET['url']!= "") {
// url not empty there is query string
$url = $_GET['url'];
$url = rtrim($url, '/');
$url = explode('/', $url);
$file = $url[0];
}
else // url empty it is root directory
{}
if ($url[0] == 'folder1'){
$path = $url[0].'/index.php';
//the path exists do redirect to folder1/index.php
header( "Location: http://www.mydomain.com/$path" ) ;
}
And as you are using a variable inside the string, you shall use double quotes instead of single ones.
You should use like this, (i.e) php variable should come out of quotes and get concatenated,
header( "Location: http://www.mydomain.com/".$path) ;
Remove the echo before redirecting - MUST
Any html out put should not be there before the
header( 'Location: http://www.mydomain.com/{$path}' ) ;
Following 2 lines will be helped you to avoid htlm before the header statement.
Put ob_start(); in begin of your code.
Put ob_end_flush() end of your code.
Thanks.