Allow only direct access on a specific folder name via .htaccess - .htaccess

I need to allow direct access on a specific folder name (and subcontents) via .htaccess, and deny all other.
To start, my current .htaccess is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
My file structure is, for instance:
modules/
core/
_test/
publics/
file.txt
other/
_whatever/
publics/
more.txt
publics/
sub/
file.txt
other.txt
.htaccess
I could not access anything, except by files and folders that are in publics folder. On this example, I could access, without problem:
modules/core/publics/ [dir]
modules/core/publics/file.txt
modules/other/_whatever/publics/ [dir]
modules/other/_whatever/publics/more.txt
modules/other/publics/ [dir]
modules/other/publics/sub/ [dir]
modules/other/publics/sub/file.txt
modules/other/publics/other.txt
I could not have access to any file or folder.
I guess that we will need regular expression, but I don't know how I can use this on mod_rewrite to works like I want.
Some suggestion?

A slight modification to your current htaccess. Any access not under a publics folder will be redirected to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !publics/
RewriteRule . index.php [L]

Related

How to separate same directory - controller name in Codigniter

I've got a controller called course and a directory with the same name, and when I request
my_site/course
it redirects my to the directory course Instead of controller, how can I redirect to the controller
P.S It's very necessary to keep directory with the same name in my apllication
Does the directory need to be int he root folder, can it not be further down the hierarchy? By having a directory with the same name as your controller in the root directory, your web server is serving up the directory instead of normal CodeIgniter routing.
If it's an absolute must to have the folder in the root, then you would need to change your .htaccess file to ignore requests which match the directory name for that directory (and any others which match controller names).
There's an answer for this already, as well as a good example on the Drupal website which I've put in below in case it gets moved
=========[ start of .htaccess snippet]==========
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/yourDirectoryName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
====================[ end ]=====================
There nativ Ci routing.
In config/routes.php
$route['course'] = 'controller/course';
after default routes

How to setup virtual directories with .htaccess

I'm trying to setup virtual directories with .htaccess.
I want to use this directories as language indicators. E.g. example.com/de-de, example-com/de-en and so on. But in fact, the directories doesn't exist on the server and the index.html file at the root of the directory should be loaded. I wrote some rules, but they dont work properly:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
URLs like example.com/de-de works, but example.com/de-de/ doesn't. When I open example.com/de-de/ it tries to load resources from the /de-de/ subdirectory, e.g. de-de/js/jquery.js instead of /js/jquery.js. Whats wrong with my rules? And is there a way to force a trailing slash, so users will be redirected from example.com/de-de to example.com/de-de/?

Rewrite folder's name using htaccess

I am wondering wether it's possible to use .htaccess to rewrite a folder name. What I mean is this.
Lets say I have a url like:
www.example.org/anyname/page.php
Now I want to rewrite the url to (for example)
www.example.org/folder1/page.php
The folder1 is an existing folder on my webspace.
important: the "anyname" is not a folder rather just a name!
Ok here is a step by step plan:
User types www.site.com/anyname/login.php.
The url should rewrite and not redirect the url to www.site.com/folder1/login.php
This means that "anyname" is just a name and not a directory. All the code should just come from folder1. Acutally "anyname" should just be an alias for folder1. I can't just rename folder1 to "anyname". Therefore I would just rewrite folder1 to "anyname".
Try adding these rules to the htaccess file in your document root:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/.]+)\.php /folder1/$2.php [L]

.htaccess - redirect from subdirectory to root directory for non existing files

Current directory structure:
wwwpublic
|+ spanish(language)
|-- index.php
|-- contact.php
|
|- index.php
|- aboutus.php
|- products.php
|- contact.php
In the root of wwwpublic directory I have files in the English language. Since site have Spanish language too, I have created separate directory named 'spanish'. Name of pages are exactly the same in both directories.
Now, some pages in spanish directory don't exist and I need to redirect request to such pages to root folder and retain name of file requested.
Example:
Visitor go to Spanish version, open index.php there, then he clicks on aboutus.php page (dont exists) inside spanish directory and then .htaccess redirects him to the /root/aboutus.php
Try something like this in your wwwpublic directory, preferably before any routing rules you may have:
RewriteEngine On
# conditions to check that current request doesn't point to a valid resource
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# condition to check that request is for the /spanish/ directory
RewriteCond %{REQUEST_URI} ^/spanish/(.+)$
# conditions to check if the /spanish/ is removed whether it would point to a valid resource
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
# all conditions match, rewrite
RewriteRule ^/?spanish/(.+)$ /$1 [L]

How to modify .htaccess so that it points to a sub-folder in codeigniter

Codeinginter provide .htaccess file as explained below. It is from CI User Guide.
However when I add this file in xampp/htdocs/mycodeigniter, and point to http://127.0.0.1/mycodeigniter/somepage, it goes to http://127.0.0.1/xampp which is the root file in htdocs.
How can I fix the file so that it points to http://127.0.0.1/mycodeigniter/somepage?
++++++++++++
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.
Try a relative substitution path:
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You may as well want to exclude the css and javascripts files from the redirection
Replace this : RewriteCond $1 !^(index.php|images|robots.txt)
With this : RewriteCond $1 !^(index.php|images|robots.txt|stylesheets|javascript)

Resources