PHP .htaccess exception for 1 file - php

I have .htaccess file + a index file with the defined url links. But now i want to except 1 php file from this rule, that i can open it with .php cause it is needed for my GET. the link is than mydomain.com/admin/user.php?id=1 but with the ending removal it is no longer working. How can i get it working ?
My .htaccess file looks like that:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action=$1 [QSA,L]
and my index.php looks like that:
session_start();
if ($_SERVER['REQUEST_URI'] == '/') {
include './dashboard.php';
} elseif ($_SERVER['REQUEST_URI'] == '/Login') {
include './login.php';
} elseif ($_SERVER['REQUEST_URI'] == '/Admin/User') {
include './admin/user.php';
} else {
include './sites/404.php';
}
Thanks for your help guys.

Related

Php get content without question mark?

I get mysite.com/?file=abc
<?php if(!empty($_GET['file'])) {?>
<?php echo $_GET['file']; ?>
<?php } ?>
it's possible get value like this:
mysite.com/file/abc
If you are using apache, and your config allows for htaccess overrides,
you can create an .htaccess file like this
# Turn on URL rewriting
RewriteEngine On
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?q=$1 [L]
URL
mysite.com/file/abc
In your php file do something like this
<?php
$query = explode('/', $_GET['q']);
if ($query[0] === 'file') {
echo $query[1];
}

PHP Local CMS CSS Error

I am creating a local CMS. Any link to the website redirects to index.php through .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</IfModule>
In the index.php file, the URL gets controlled. If the path begins with admin then admin.main.php gets included.
if (isset($_SERVER['PATH_INFO'])) {
$request_url = $_SERVER['PATH_INFO'];
while (substr($request_url, -1) == '/') {
$request_url = rtrim($request_url, '/');
}
$explode_url = explode('/', $request_url);
if (isset($explode_url[1]) && $explode_url[1] == 'admin') {
require('includes/admin/admin.main.php');
}
}
The admin.main.php controlls if request is /admin/pages, and if so another file gets included.
global $request_url;
if ($request_url == '/admin/pages') {
require('includes/admin/panel/panel.pages.php');
}
In the head of panel.pages.php a link to the CSS-file is included with the path includes/admin/CSS/panel.pages.css. The problem is that if you go to http://localhost/cms/admin/pages the HTML and PHP code runs, but not the CSS or the JS (which is included in the bottom). In the Chrome Developer Tool, the href of the link to the CSS is http://localhost/cms/admin/pages/includes/admin/CSS/panel.pages.css.
To mention is also that ../ can be done multiple times before the href, but with the same result and link. In the CMS-directory (base), there is no directory with the name admin, the directory to admin-folder is includes/admin.

PHP htaccess wrong with URL's

http://localhost/customers/website/jobs/28
When for an example the URL above is visited and my code is used, it goes to: http://localhost/customers/website/jobs/startpage
if(isset($_COOKIE["startpage"])) {
}else{
header("Location: startpage");
}
But if I do header("Location: /startpage"); it becomes http://localhost/startpage
My Htacess is:
RewriteEngine On
RewriteBase /customers/website/
RewriteRule ^startpage/?$ startpage.php [NC,L]
How can I make it go to http://localhost/customers/website/startpage?
Don't use PHP for this. You can do this in htaccess itself:
RewriteEngine On
RewriteBase /customers/website/
RewriteCond %{HTTP_COOKIE} !startpage
RewriteRule !^startpage/?$ startpage [L,NC,CO=startpage:1:%{HTTP_HOST},R=302]
RewriteCond %{HTTP_COOKIE} startpage
RewriteRule ^startpage/?$ startpage.php [L]
and remove your header function call from PHP code.
If you will want to do this in PHP itself then use:
if(isset($_COOKIE["startpage"])) {
//
} else {
header("Location: " . $_SERVER["BASE"] . "startpage");
}
and keep same rewrite rule shown in question.

Htaccess rewrite rules for this url

I just started to learn htaccess and i'd like to rewrite my current urls from this:
http://www.url.com/?location=script
To:
http://www.url.com/script
So far i've managed to do this but now i want to have a directory with more controllers so i can have something like this:
http://www.url.com/script/method
Structure: Script directory --> method.php
Currently my directory structure for includes its like this:
assets-->client(directory):
login.php
logout.php
register.php
something.php
And i'd like to access these using a url like:
url.com/client/login
url.com/client/logout
url.com/client/register
url.com/client/something
My .htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?location=$1 [L]
</ifModule>
PHP based inclusion code:
####################################################################
# PARSE THE CURRENT PAGE #
####################################################################
$includeDir =".".DIRECTORY_SEPARATOR."assets/controllers".DIRECTORY_SEPARATOR;
$includeDefault = $includeDir."home.php";
if(isset($_GET['ajaxpage']) && !empty($_GET['ajaxpage'])){
$_GET['ajaxpage'] = str_replace("\0", '', $_GET['ajaxpage']);
$includeFile = basename(realpath($includeDir.$_GET['ajaxpage'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath)) {
include($includePath);
}
else{
include($includeDefault);
}
exit();
}
if(isset($_GET['location']) && !empty($_GET['location']))
{
$_GET['location'] = str_replace("\0", '', $_GET['location']);
$includeFile = basename(realpath($includeDir.$_GET['location'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath))
{
include($includePath);
}
else
{
include($includeDefault);
}
}
else
{
include($includeDefault);
}
All my controllers are in assets/controllers/ucp/login.php for example.
How about:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(/client/[a-zA-Z0-9_\-]+)$
RewriteRule ^[a-z]+ index.php?location=$2 [L]
</ifModule>
It does include a leading slash and doesn't have the PHP extension. But this can be altered in PHP to suit your needs.
Be wary though, your code gives access to all your PHP files. You might want to check $_GET['location'] against an array of allowed locations. Consider the following URL as example of how this could go wrong
http://example.com/index.php?location=../drop_database.php

Hide php page name of header function in url

I'm trying to hide the php page name in url which redirects after login by header function. So far I can hide the index file but can't hide the file which redirects after login. Here are my codes,
PHP script for after login events
$sql_login = mysql_query("SELECT * FROM sms_people WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($sql_login);
if ($row > 0 && $row[5] == 1) {
header('Location: adminpanel.php');
}
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
How can I hide the files which redirects from header function by using .htaccess file? Need this help badly. Tnx.
there is an alternative way to redirect php files and hide them
for example
http://yourwebsite.com/user/login/
user part main user.php or you can change the file name
firstl, in your htaccess redirect to index php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
############### SEO ##########################
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
in your index.php file
$rootfolder="parts"
if(isset($_GET["url"])){
$part= explode("/", $_GET["url"]);
if(isset($part[0]))
$controller = ''.strtolower($part[0]).'';
if(isset($parca[1]))
$method = ''.strtolower($part[1]).'';
if(isset($parca[2]))
$id = ''.$part[2].'';
if( file_exists($rootfolder."/".$controller.php))
{
//you can call the file if it is exits
requre_once $rootfolder."/".$controller.php;
}else{
die("404 not found !");
}
}
Ok got a solution. I changed the information in header function & placed the file corresponding to the given information in .htaccess file. Here are the codes,
PHP Script
if ($row > 0 && $row[5] == 1) {
header('Location: AdminPanel'); // Instead of adminpanel.php
}
.htaccess
RewriteRule ^AdminPanel/?$ adminpanel.php [R,NC,L] // Added this line.
Tnx for all your efforts btw.

Categories