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.
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.
Comments