.htaccess mod_rewrite in main directory and subdirectory - php

I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php

Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory

Related

No input file specified -LAMP(Apache)

Anyone can help with my htaccess file, its working with the root domain directory folder only, if i put the index file into the root domain directory and access page like this https://example.com/edr/login , it works nicely . However when i create folder inside it and try to access site like this , https://example.com/createdfolder/edr/login, it says 'No input file exist. Below is my htaaccess file codes. My $_SERVER['QUERY_STRING'] is set to 'login'
Options All -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
If to copy WordPress' .htaccess it would look like this:
RewriteEngine On
RewriteBase /createfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /createfolder/index.php [L]
Of course you can replace createfolder/ with your/path/

How show contents if the folder does not contain index.php/.html using .htaccess?

My set of folders looks like this:
server
--public
----index.php
----/items/
------index.php
------template.php
------/folder1/
------/folder2/
------/folder3/
--------index.php
------/folder4/
When this happens, folder3 in the browser shows the content at site.com/items/folder3/.
My task is to display in /folder1/, /folder2/, /folder4/ the content from the file site.com/items/template.php (because these folders do not contain index.php or index.html). How can I do this?
===========================
I tried this way, but it has disadvantages - These rules also work with non-existent directories (as /items/aaa/bbb/zzz/), which is inappropriate for me.
DirectoryIndex index.html index.htm index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.htm !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.php !-f
RewriteRule ^(.+?)(?:/[^/]+)?/?$ template.php [L]
You can find your answer here
https://stackoverflow.com/a/22412283/2118611
You should rewrite your empty folders to template.php
Try something like this:
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to template.php
RewriteRule .* /items/template.php [L]
EDIT:
To check if index file exist you can try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index\.php !-f
RewriteRule . /template.php [L]
The above code
RewriteCond %{REQUEST_FILENAME} !-f check if the requested uri is not a file.
RewriteCond %{REQUEST_FILENAME} -d check if the requested uri is a directory.
RewriteCond %{REQUEST_FILENAME}/index\.php !-f check if the index.php is not found.
Then redirect to /template.php

Rewrite ALL requests to a folder with .htaccess file

I'm trying to rewrite all requests in a folder like this:
https://www.test.com/banana/apple (or whatever) goes to https://www.test.com/public but in the url is still https://www.test.com/banana/apple.
How can I do this?
Inside that specific folder you can create a .htaccess with this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /public/ [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /public/index.php [NC,L,QSA]
This .htaccess will rewrite all requests to /public/index.php. Existing files and directories will still be served.

How to load default index file with .htaccess for variable directory structure in url

Lets say my site structure is like this:
www.mywebsite.com/directory/$variabledirectoryname/$variableproductlink
As you can see in the middle i have 2 variables in the url, but the last is the poduct link. My index.php file stays in www.mywebsite.com/directory/$variabledirectoryname folder. How to load it?
I have this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php [L]
But it doesnt work.. Thanks !
You can use this rule in /directory/.htaccess:
RewriteEngine On
RewriteBase /directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/[^/]+/?$ $1/index.php [L]
Update:
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(directory/[^/]+)/[^/]+/?$ $1/index.php [L]

How to allow .htaccess if you are in folder

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This is my current .htaccess. It doesn't work when I put it in a folder (it works otherwise). It just gives Not Found page. Why is that?
Because for a sub directory you need to specify the path like
RewriteBase /subdir/
in the top of your file, right after RewriteEngine On

Categories