Use .htaccess to prevent root directory from public access - php

I don't know such thing is possible or not practically, but let's give it a try. In past (more than three years ago), I came to know about solution like this (don't know exactly how it is), in one MVC tutorial.
Directory structure
WebRoot/
includes/
config/
public/
images/
js/
css/
index.php
init.php
What I Want To Do Is
When user access http://www.mysite.com/a/b/c/, redirect all requests to /public/index.php page. i.e., all requests to website will go through /public/index.php page, except images/, js/ and css/ directories, which are in public/.
Also, http://www.mysite.com/includes/ will redirect to index.php page, and get includes/ as $query.
Index.php page
<?php
// get init file from webroot directory, which will load all required php files.
define ('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . 'init.php');
// get query from url (here, $query = "a/b/c/")
if(isset($_GET['q'])) {
$query = $_GET['q'];
}
// insert images, css etc. resource
echo "<img src='http://www.mysite.com/images/foo.jpg' />";
echo "<script src='http://www.mysite.com/js/bar.js'></script>";
?>
This will be useful for them who are using web-hosting, which doesn't provide one step up directory access to webroot.
EDIT
OK, I found the site from where I got this concept. See two .htaccess files used in root directory and public directory. Link to MVC tutorial

I assume the .htaccess is in WebRoot.
To rewrite all requests, you just use RewriteRule. When you want to exclude some paths, you use one or more RewriteCond
RewriteEngine on
RewriteCond %{REQUEST_URI} !/images/
RewriteCond %{REQUEST_URI} !/css/
RewriteCond %{REQUEST_URI} !/js/
RewriteCond %{REQUEST_URI} !/public/index\.php
RewriteRule .* public/index.php?q=$0 [L,QSA]
RewriteRule ^images/.+ public/$0 [L]
RewriteRule ^js/.+ public/$0 [L]
RewriteRule ^css/.+ public/$0 [L]
The flag QSA appends any other existing query string as arguments to index.php. If you don't want to append any existing query string, just leave the QSA flag out.
If you want the rewrite to be visible to the client, you must add the R flag to the RewriteRule, i.e. [R,L,QSA] or [R,L].

You can use another tricks, where your page will not redirected to another page but you will restrict other user to see the directory index
Options -Indexes
Add this text in your .htaccess file, it will generate a 403 errror (forbidden)

Related

htaccess RewriteRule using slash as get params: can't access other files in the folder

I have a simple user system in my site. The URL works like this: /profile/user?id=user_id
I wanted to do something like /profile/user/user_id instead, and I found the following code to use in htaccess.
RewriteRule ^user/(.*)$ user.php?id=$1 [L]
The code does work as intended, but after doing that I found out that I can't access any other file inside of the profile directory besides the index file. All of them return a 404 error.
There's a couple more of important pages in there, so I would need to access them. I can't access user.php without an ID either, but that's not my main concern, even though I would also like to access it if possible.
I'm not really used to use RewriteRule, so I'm not really sure what's going on here. What can I do?
EDIT: I was asked to give more info, here we go.
The profile folder full route is /en/profile
Structure of the profile folder:
profile/settings.php (directory index)
profile/user.php
profile/change_avatar.php
profile/change_avatar_upload.php
profile/change_email.php
profile/change_password.php
profile/custom_utgen_characters.php
profile/custom_utgen_character_editor.php
profile/reset_password.php
Other lines in the htaccess file:
DirectoryIndex settings.php
htaccess file in the root folder of the website:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
This is all the relevant option I could get, I hope it helps.
I tested your rewrite rule and directory structure and had no problems to access files in the profile directory. Can you provide more information like the complete .htaccess file and the directory structure laying under profile?
EDIT: The .htaccess in your profile folder overwrites the .htaccess in your root folder. To prevent this, you have to add the line
RewriteOptions inherit
to the .htaccess file in the profile folder.

RewriteRule for friendlier URLs

I have a site that has a news section in a subfolder which I have been adding to manually.
www.example.com/subfolder/news-and-views/name-of-article.php
I recently made a rather simplistic CRUD feature to make adding these articles easier.
I have:
www.example.com/subfolder/news-and-views/index.php - This displays all articles
www.example.com/subfolder/news-and-views/article.php - This takes an Id parameter from a $_GET request
The url I end up with when I view an article is:
www.example.com/subfolder/news-and-views/article.php?Id=name-of-article
I did some reading and used http://htaccess.mwl.be/ to test a RewriteRule
The rule is: RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
However when I go to news-and-vies/ (the index page) my CSS is stripped out and nothing is displayed as my paths are changed.
Also it seems to go to article.phpid?= without passing anything, rather than just using the actual index page.
To set paths I use a variable called $path2root which is the number of directories to the root.
E.g $path2root = "../../";
So: I'd have include $path2root . "includes/head.php"
Is my RewriteRule interferring with this variable?
My .htaccess file
RewriteEngine On
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
My file structure
/ .htaccess at this level (root)
--/ subfolder
----/ news-and-views
------/ index of news and views
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
As mentioned in my comment, you appear to have omitted the /subfolder, so I'm not sure how this rewrites successfully at all? You also need to be careful of rewriting existing files (eg. index.php and article.php and CSS?) and creating a rewrite loop.
Try something like the following instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(subfolder/news-and-views)/(.+) /$1/article.php?Id=$2.php [L]

Mod-Rewrite and .htaccess Problems

Having some issues with Mod_Rewrite. I must admit this is my first venture into the module so I could be going through something silly. I have been trying to get this to work on my own for a day or so off and on and I can not figure out why this is not working.
I am getting the following errors:
Notice: Undefined index: Dir in D:\wamp\www\WildForFashion\Portal\Index.php on line 31
What I am trying to accomplish is to be able to have one template page (index.php) and have Mod_Rewrite change the url for me so it can be Search Friendly and to also ease coding and design.
I was also having issues when it came to no Images / CSS files loading at all, I did read that by using an absolute url would work.
As of right now I am working in the PORTAL directory first (which is an admin page for website maintenance) and then will be moving onto the root directory. I am not sure if I need to have one .htaccess file or two (one being in root dir, second being in portal dir).
The DIR folder is to hold each category and page for the website like the following:
Domain.com/{CATAGORY}/{PAGE}/
ROOT / DIR / {CATAGORY} / {Page}.php
Domain.com/Portal/{CATAGORY}/{PAGE}/
ROOT / Portal / DIR / {CATAGORY} / {Page}.php
.htaccess Before
RewriteEngine on
RewriteRule ^/Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule ^Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule ^Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
.htaccess After (Working w/ Exceptions)
RewriteEngine on
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/([^/]+)/$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
Index.php
<?php
include_once("DIR/" . $_GET['Dir'] . "/" . $_GET['Page'] . ".php");
?>
Directory Tree
CSS
DIR
IMG
JS
SRC
Portal
CSS
DIR
Portal
Inventory
Stats
Orders
IMG
JS
SRC
Index.php
.htaccess
Index.php
You are including forward slashes in the matching rule that don't need to be there and excluding them from the redirect path where they do need to be. Try this:
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
Note I removed the ^ from the matching portions of the rewrite rules as this indicated taht should be the beginning of the URI. I also removed the "/" in front of the substritution pattern.

.htaccess in subdirectory

I've been searching the whole evening for a solution/approach for my problem with my .htaccess file.
Basically I have www.site.com with a .htaccess in the www directory with the following content:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/admin)
RewriteRule ^[^_+/]. index.php
It works just fine and allows www.site.com/en/articles/Interesting_title/3 to be parsed by index.php which reads which controller, language and what article to display.
However I'd like for the admin system to work the same way. www.site.com/admin should have an .htaccess files that allow me to write URL's this way.
www.site.com/admin/en/articles/article_title/3 should allow me to edit article number 3 using en english UI.
Dumping $_SERVER['REQUEST_URI'] in the first case gives "en/articles/Interesting_title/3"
Dumping $_SERVER['REQUEST_URI'] in the second case gives "admin/en/articles/article_title/3"
If I at a later point choose to move administration to www.site.com/shassooo I would like to avoid changing any code other then the .htaccess files.
Cheers
Append this line in the same .htaccess file you have, not under admin sub directory:
RewriteCond %{REQUEST_URI} !^/admin/index.php$ [NC]
RewriteRule ^admin/[^_+/]. /admin/index.php [L,NC]

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories