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>
Related
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 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.
my current url : http://example.com/blog-single.php?id=15
this is a dynamic blog page the "id" has to be change accordingly to blog adding
I need to change this url to http://example.com/blog-single/<coresponding-blog-name>
eg: http://example.com/blog-single/my-new-blog
how to Remove .php and id from the current URL?
First of you need to create a .htaccess file and add this code in it
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1
After that in your main file for example in index.php file you need to add the code
<?php
$key=$_GET['key'];
if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
try this according to your need.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]
here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.
For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:
RewriteEngine On
RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]
This tool will help you: http://www.generateit.net/mod-rewrite/index.php
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'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.