.htaccess redirect to index.php in any directory

Justin

i have been working on a framework that i am creating in php and the essential part for it to work is to send all the url queries to index.php in the root folder. I managed to do it in the folloowing code at my .htaccess file

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
#RewriteRule ^(phpmyadmin)($|/) - [L]
RewriteRule ^(html)($|/) - [L]
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php

The problem occurs when i move the site to a different folder for example

www.mydomain.com/subdir/{where the index.php is, along with htaccess}

and it stops working here. The same happens if i move the framework application to a sub-domain as well.

so I am trying to modify .htaccess to rewrite to index.php properly where the .htaccess file is at regardless if it is in a sub-directory or sub-domain. How can i get the .htaccess to know that application is in a sub-directory and rewrite to it properly so change of location does not break .htaccess pointing to the right file?

 www.domain.com/{.htaccess+index.php + framework} => works properly
 www.subdom.domain.com/{.htaccess+index.php + framework}  => does not work
 www.domain.com/subdir/{.htaccess+index.php + framework} => does not work //send to www.domain.com/index.php
 localhost/projectname/{.htaccess+index.php + framework} =>does not work

As you can see it needs to send the requests to index.php where htaccess is also located at.

anubhava

You can use this trick to always write in index.php of the current directory:

RewriteEngine On

# Generate BASE dynamically
# It compares REQUEST_URI variable (which is complete path) with the URI matched by 
# RewriteRule (which is relative to current path) and gets differential in
$ %{ENV:BASE} variable.
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

RewriteRule ^(html)($|/) - [L]

# Redirect requests to %{ENV:BASE}index.php which is index.php of current directory
# It also makes sure index.php exists in current directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.php -f [NC]
RewriteRule . %{ENV:BASE}index.php [L]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

htaccess - redirect a directory including the index page

Redirect all to index.php using htaccess

.htaccess redirect subdomain to directory

.htaccess Redirect all html files to index.php

htaccess redirect subdomain to directory

Redirect index.php with parameters to a folder and remove parameters using htaccess

Redirect to index.php and remove trailing slash .htaccess

htaccess - redirect rule on directory

.htaccess - Redirect All URLs + Existing Directories to index.php

htaccess redirect to index.php & force https/www problems

.htaccess redirect not working with index.php of sub directory

PHP: How to redirect if any file path is accessed directly in .htaccess

Redirect everything BUT index.php with htaccess

How to redirect requests for index.php to /home with htaccess

Redirect all requests to index.php WITHOUT .htaccess

Redirect Rule not working for index.php in htaccess

Redirect directory to file .htaccess

how to redirect the user if there are any directory name in the url htaccess

htaccess redirect where index.php? exists

htaccess - redirect directory but not sub directory

htaccess multiple directory redirect

htaccess redirect from index.php to / only in specific folder

.htaccess: redirect to sub directory

Redirect a page to index in .htaccess

htaccess is denying access to index.php as directory index

.htaccess - remove index.php, redirect with multiple parameters

htaccess redirect directory but not files in directory

htaccess redirect to a directory

Redirect with htaccess from index.php to another.php file