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.
Related
Currently I works on to transfer my site into SEO friendly URL (in localhost),
Here's the original URL with query string:
http://{ip}/sitename/item.php?category=44
I want convert to:
http://{ip}/sitename/item/category/44
.htaccess file (same directory with item.php):
DirectoryIndex index.php
Options -Indexes
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ item.php%{REQUEST_URI} [L]
RewriteRule ^/item/([0-9]+) /item.php?category=$1
</IfModule>
<Files *htaccess>
Deny from all
</Files>
in item.php, I use $_GET['category']
$category = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_GET['category']));
$list = $listing->get_items($category, $mysqli);
if($list == 0){
echo '<p><h2>Page not found</h2></p>';
die();
}
Problem 1:
When I loaded http://{ip}/sitename/item/category/44, the page cannot get the variable passes to get_item(), the page is shown Page not found? 44 is suppose return a value.
Problem 2:
My page doesn't loaded referrer files, all *.css *.js etc are just ignore?
Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule item/category/(.*) item.php?category=$1
PROBLEM 2 - The problem has arised as you have not mentioned the base path.
You need to assign base path to load css and other references.
Try using this in <head> tag <base href="http://www.MYSITE.com/" />
This will load your css/js.
Problem 1: No rewriting has happened here – (you only think it has, beause your script item.php got called anyway, because you had item in the URL and MultiViews has done its work) – paths the RewriteRules match on in .htaccess never start with a /, so remove it.
Problem 2: Has been discussed many times before (and should also be obvious to anyone who knows the basics of how completion of relative URLs works) – see f.e. .htaccess URL Rewrite Problem (Scripts don't load)
RewriteEngine On
RewriteBase /sitename
RewriteRule ^item/([0-9]+)/?$ item.php?category=$1 [L,NC]
I have an index.php and news.php and .htaccess files in my localhost/DIRECTORY/AID/
folder, and I am basically trying send/receive data from index.php to news.php
This is a function inside the index.php, which creates a link from database query, and echos out a title of an article.
function news_preview() {
$query = "SELECT * FROM updates ORDER BY update_id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$url = "/news/$row[update_id]/" . preg_replace('/[^a-zA-Z0-9-_]/', '_',
$row['update_title']);
echo " " . substr($row['update_title'], 0, 26) . "...<br/>";
}
}
echo news_preview();
Now, here is what the .htaccess looks like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
Now, to the problem. Basically, when I clicked on the link (generated by news_preview() )
shown in the index.php, All I get in the news.php page is nothing. But, probably because I am trying to use the $_GET['title'] Although, I am not certain if that is how we retrieve data. But, the links take me to http://localhost/news/46/This_is_news_title
which is perfect, but I am getting the Object Not Found error
Below, is the image of the error I am getting.
Put this code in the htdocs/.htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+)/.*$ /DIRECTORY/AID/news.php?id=$1 [QSA,L,NC]
In the AID/ folder along with index.php & news.php The problem, is I don't know how to get the data from the url in the news.php
The htaccess file needs to be in your document root. When the request URI is in the form:
/news/1234/something-something
The order apache uses to resolve whether overrides (i.e. stuff in htaccess files) should be applied is first see if this is a directory /news/1234/something-something and if so, if there's an htaccess file in it. That's not a directory so apache moves on. If /news/1234 is a directory, and if so, see if there's an htaccess file in it; since it's not, nothing happens. Then apache checks if /news is a directory and if so, check for htaccess; it's also not a directory so nothing happens. Finally, apache checks the document root / to see if there's an htaccess. Since the document root is a directory, that's where you need to put your rules.
The /DIRECTORY/AID/ directory is never in the mix here, unless that is actually where your document root is. If DIRECTORY/AID/ is your document root, e.g. the URI / maps directly to that directory, then you need to change your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ news.php?id=$1 [QSA,L]
Sounds like you're expecting to be able to read the last portion of the URL as $_GET['title'], but your htaccess rule isn't adding it to the query string.
Try changing
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
to
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1&title=$2 [QSA,L]
I use the code below to basically go to my pages. How it works is I put index.php?req=pagename and it will check my protected folder to see if the file is there if it is then it goes there. I need a mod rewrite so that it doesnt show all that index.php?req=pagename and just shows /pagename
require_once("protected/header.php");
if (isset($_GET['req'])) {
$req = $_GET['req'];
} else {
$req = "overall";
}
require_once("protected/$req.php");
require_once("protected/footer.php");
Your code allows any php file (barring safemode/open_basedir restrictions) to be parsed and executed. You need to escape that input first, even if it's something as rudimentary as removing slashes, tildes and periods.
As far as rewrite goes, simply create a .htaccess file in your document root along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?req=$1 [QSA,L]
</IfModule>
You will have to use POST instead of GET if you want a clean dynamic url. Your question though doesn't make much sense.
For example:
HANDLER *.css --> process-css-files.php
HANDLER *.(jpg|png|gif|jpeg) --> process-image-files.php
In addition how to:
if (*.css EXISTS) then
include( THAT_FILE )
else
process_URL-->( process-css-files.php )
I use something like this for combining css scripts within the css folder to a single concatenated file:
RewriteRule ^css/(.*\.css) /combine.php?file=$1
For the most part, you're going to use mod_rewrite to redirect URLs. You should put the following in your .htaccess file (if your server has support for it):
RewriteEngine on
RewriteRule ^/?(.+)\.css$ process-css-files.php?name=$1
RewriteRule ^/?(.+)\.(jpg|jpeg|gif|png)$ process-image-files.php?name=$1&extension=$2
That would solve the first part of your question (I think). I'm sure there's a way to get .htaccess to check if a file exists before rewriting the URL, but I'm more experienced with PHP, so I'd probably just always redirect to the PHP file, and then have PHP do all the checking and whatnot:
<?php
$name = $_GET['name'].'.css';
header('Content-type: text/css');
if (file_exists($name)) {
echo file_get_contents($name);
} else {
// do whatever
}
?>
I believe all this can be handled by .htaccess rules. Try something this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^([^.]*\.(jpe?g|png|gif))$ process-image-files.php?img=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*\.css)$ process-css-files.php?css=$1 [L,NC]
Ok so I have this site.... with the url
http://posnation.com/
and there are alot of pages that i need to save the url structure for....like this
http://posnation.com/restaurant_pos
http://posnation.com/quickservice_pos
http://dev.posnation.com/retail_pos
ext....
The problem that i have now is that i want to save the same url for all these pages and I am looking for the best approach. The way its working now is its done with a code in miva and we are getting off miva.... I know I can create a folder named restaurant_pos or whatever the url is and create an index.php in there.This approach will work but the problem is I need to do this for 600 different pages and I dont feel like creating 600 folders in the best approach.
any ideas
You should use .htaccess to route all the requests to a single file, say index.php and do the serving from there based on the requested URL.
The following .htaccess file on the server root will route all the requests to your index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
Now you should parse the $_REQUEST['route'] to identify which file you should serve. Here is an example that will serve a page based on the last element of the URL (ex: pos):
<?php
$parts = explode($_REQUEST['route']);
if ($parts[count($parts) - 1] == 'pos') {
include "pages/pos.php";
}
Definitely you'll need to write your own logic, the above is just an example.
Hope this helps.
Usually the easiest way to do this is to create an .htaccess file that redirects all requests to /index.php. In /.index.php you analyze the URL using probably $_SERVER['REQUEST_URI'] and include the appropriate content.
Heres a sample htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
In your /.index.php do something like ... (this is just a VERY simple example)
require 'pages/' . $_SERVER['REQUEST_URI'] . '.php';
Now if someone goes to http://posnation.com/restaurant_pos pages/restaurant_pos.php will be included.
pages/restaurant_pos.php could include the header and footer too.
<?php require( HEADER_FILE ) ?>
restaurant_pos content
<?php require( FOOTER_FILE ) ?>