2

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.

4
  • change /index.php to just index.php? Of course now you'd need to have an index.php in EVERY directory. Commented Aug 29, 2014 at 18:27
  • 1
    Is using RewriteBase not an option for you? With it you could easily fix the issue by changing 2 entries on your .htaccess when you need to move if you need to move. RewriteBase /folder_it_is_now/ and your /index.php into index.php at the RewriteRule. Commented Aug 29, 2014 at 18:58
  • @Prix Can you provide any samples? i am not really understanding. Commented Aug 29, 2014 at 19:01
  • @Justin here is a very simple example pastebin.com/vR7zLwD8 if you have your application running on a subdir, that is how it would look like and as you can see all you really change is the RewriteBase and the index.php checks. If that is usable to your situation I will turn that into a answer, but if you don't want to change any files or anything I will just leave it as a comment. Commented Aug 29, 2014 at 19:04

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

9 Comments

+1 Very interesting ENV, don't you need to check it against %{DOCUMENT_ROOT} to be certain the index.php exists?
@Prix: Thanks, Yes it will be a good check to make sure index.php exist. I will add that in my answer.
Also I think it would be really nice if you could expand an explanation on what happens on the first rule, as it will loop to find the proper base, against the last rule which will stop when index.php is found.
This works when I test it on localhost. Let me try it on the real server to see if there is any problems so everyone can use this for their needs.
@anubhava this fixed my problem with the location of the application. do you mind giving me a link to see where i can learn about your method for this answer?
|
3

Edit based on new examples:

How about something like this? Basically check to make sure it's not a file, If not a file redirect to index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php [PT,L,QSA]

3 Comments

I see that would fix the problem but i am trying to achieve this with .htaccess figuring it out itself.
Unless you have some other special condition there is no way that apache will know where the new subdir is. Do you have examples of the URLs you are trying to use? Are they following a special pattern?
i just added the sample urls for your reference. Please have a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.