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.
Related
This question already has answers here:
Seo Friendly URL results in CSS IMG and JS not working
(2 answers)
Closed 3 years ago.
I'm running a localhost on my Mac using the native sites, and I can't seem to get .htaccess CSS working. I'm using an index.php page, that is then given url variables to tell which page to display (ex. index.php?login). There two issues I'm facing here: The first is that it redirects to a page where my global styles aren't included, and the second is that it shows the homepage instead of my login page.
I've tried navigating to the rewritten site as localhost/index.php?login, and all the CSS and HTML displays fine. It's only when I rewrite it as localhost/login that it fails. It shows my homepage without any of the styles that should be include from index.php
Here is my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex index.php
RewriteRule ^login$ index.php?login [NC]
</IfModule>
Here is my index.php
//Check for more GET variables.
$url = $_SERVER['REQUEST_URI'];
$split = explode("?", $url);
if (isset($split[1])) {
$newvarsarray = explode("&", $split[1]);
foreach ($newvarsarray as $newvar) {
$keyandvalue = explode("=", $newvar);
if (isset($keyandvalue[1])) {
$_GET[$keyandvalue[0]] = $keyandvalue[1];
} else {
$_GET[$keyandvalue[0]] = '';
}
}
} else {
//No additional vars, leave $_GET alone.
}
include_once 'globalheader.html';//Imports styles and things
include_once 'header.html';//Stylized header
//This is the area where we handle the different states of the webpage and import them into here
if (empty($_GET)) {
//Main homepage
include_once 'mainpage.html';
} else if (isset($_GET['login'])) {
//Login page
include_once 'login.html';
} else if (isset($_GET['register'])) {
//Register page
include_once 'register.html';
} else if (isset($_GET['profile'])) {
//Profile page
}
include_once 'footer.html';//Stylized footer
include_once 'globalfooter.html';//Closes things from globalheader.html
?>
I want it to display the correct page with correct CSS instead of seemingly skipping over the entirety of index.php and just using the .html file.
tl;dr you are missing [OR].
A file cannot be "not a file nor a directory" at the same time.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login$ index.php?login [NC]
</IfModule>
I have a website and I want the redirect all files to index.php file. But I have problem with folders. domain.com/panel/ doesnt work. but domain.com/panel/index.php works.
in localhost with wampserver, works good. But in hosting doesnt work.
Here is my htaccess file:
RewriteEngine On
RewriteBase /
# Site yönlendirme motoru
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-\s]+)/?$ index.php?url=$1 [NC,L,QSA]
RewriteRule ^(.*?\.(?:html?|php))$ index.php?url=$1 [QSA,L,NC]
Not: I want only html and php files because, if i dont do this, images, css, js files doesnt work. Is there any another way? How can i redirect all files to index.php while images, css, js files still works?
Not 2: I want panel folder to redirect because of security. like this:
$url = $_GET['url'];
$url_array = explode('/', $url);
if($url_array[0] == 'panel') {
if($url_array[1] != 'login.php' and (!isset($_SESSION['user']))){
header('Location: login.php');
die();
}
include($url);
}
edit:
What i have to make for except to images, css, js files on htaccess?
I need to hide the profile.php from appearing in the URL.
So here's what I have:
The URL I am accessing:
sampleuser.domain.com/profile.php
How I get the user 'sampleuser':
$url = "//".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
/*this will give me $url = '//sampleuser.domain.com/profile.php'; */
function get_user_from_url($url)
{
if (strpos($url , 'www.') !== false) {
preg_match('/\/\/www.(.*?)\.domain/', $url, $val);
}
else
{
preg_match('/\/\/(.*?)\./', $url, $val);
}
return $val[1];
}
$user = get_user_from_url($url);
echo $user;
I just need to hide the profile.php, also the thing to consider is if we hide the profile.php, it might conflict with the index.php.
But luckily we will only access the index.php if the URL is only domain.com (without user).
I'm low on htaccess knowledge, can anyone give me the direct answer, and I would be very appreciative if you can add some explanation, it'll give me a head start on learning htaccess.
So basically, what I was making is a dynamic subdomain. So I added a subdomain on my DNS:
*.domain.com
then added these on my htaccess
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^/?$ /agent/index.php?user=%2 [L]
</IfModule>
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
I try to make a single entry point site. here is my routing in the index.php
$page = 2;
$command = 3;
$requestURI = explode("/", $_SERVER['REQUEST_URI']);
if (!$requestURI[$page]) {include_once ('home.php');}
else if (file_exists($requestURI[$page].".php")) {include_once ($requestURI[$page].".php");}
else {include_once ("404.php");}
my .htaccess
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
if i go domain.com/subfolder/gallery it works fine the gallery.php is included, but if i go domain.com/subfolder/gallery/subfolder my js includes are messed up it look for them in
domain.com/subfolder/gallery/js/
instead of at they right place:
domain.com/subfolder/js/
i include them like this:
html
of cource all the css and img files are included wrong too. how can i fix it?
Use absolute file paths..
Your code is also vulnerable to Local File Inclusion.