How to get webiste root - .htaccess

I want to include a file using .htaccess, the file is opened on the
Homepage but not in other places like folders.
Here is my code block on .httaccess
php_value auto_prepend_file "Main/API.php"
the error message
Fatal error: Unknown: Failed opening required 'Main/API.php'

In other folders, the string "Main/API.php" means somewhere that can't be reached.
Either place a different .htaccess in those folders, or try using absolute paths (i.e. "/var/www/www.site.com/htdocs/Main/API.php", or "/Main/API.php").

Related

A simple .htaccess rewrite rule is not working as expected if php files are requested

I'm trying to rewrite all requests directed at files/folders in the root directory to a subdirectory called "Web". This simple .htaccess code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/Web
RewriteRule ^(.*)$ Web/$1 [QSA,L]
is working fine for most scenarios, but, unexpectedly, php files directly accessed through the browser are still being looked for in the root directory, even though the RewriteRule does not discriminate between file types (it get's weirder though, see below). With this, I'm getting the following results...
Working:
Directories: domain.com/test/ will correctly rewrite to domain.com/Web/test/ independent of whether the folder exists in either the root or the Web directory or both, as expected
All tested non-php file types, e.g. domain.com/test.txt correctly rewrite to domain.com/Web/test.txt, again independent of whether the file exists in either the root or the Web directory or both, as expected (i.e. either displaying the file that I placed in the Web directory or throwing the expected 404 error message for not finding the file in the subfolder if it does not exist)
HOWEVER, trying to open php files in the browser (e.g. domain.com/test.php) results in the following, unexpected behavior:
If the php file DOES exist in the Web subfolder but does NOT exist in the root directory (default scenario), I'm getting an unexpected 404 "The requested URL /test.php was not found on this server", i.e. the server is looking for the file in the root directory, ignoring the .htaccess rewrite, even though the rewrite clearly is working for other file types, see above
If the php file does NOT exist in the Web subfolder but DOES exist in the root directory, I'm getting a 404 "The requested URL /Web/ test.php was not found on this server.", i.e. NOW the server is trying to open the file from the expected subfolder-location.
If the php file DOES exist in the Web subfolder AND in the root directory, the server displays the test.php file version that was placed in the subfolder.
My conclusions are:
all non-php file types are behaving according to the .htaccess file, independently of whether the requested files/folders exist in the root directory and/or the subfolder - the behavior is always as expected
all requests for php files follow the htaccess rule ONLY IF a corresponding file exists in the root directory, even though the file residing in the root directory is never actually served to the browser, in accordance with the rewrite rule
I find this extremely weird, since there's no RewriteCond to specify absence or presence of the file. The Regex clearly triggers for php files, but only in a complicated, counterintuitive fashion. The one RewriteCond I do have only prevents infinite looping.
I have tried adding RewriteCond's that match both absence and presence of file, but it did not fix the file problem.
Does anyone have an idea? Why are php-files treated differently from all other types in this scenario?

Missing htaccess file on server?

i've just created a website. It's hosted by GoDaddy and I use cPanel to manage it.
all of my files relating to my website are stored in a public_html folder.
The problem I am having is that I cannot seem to access different pages I have uploaded, I'll show what I mean.
www.website.com --> works fine (defaults to home.html)
www.website.com/About/about.html --> works fine
www.website.com.com/About --> I get the error below:
"You don't have permission to access /About/ on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request."
I would like website.com/About to display about.html.
I think it may be something to do with the .htaccess file? I may of clumsily deleted it at some point.
I have tried to set permissions to 777 on all my public_html files, but I get the same error.
I have tried to locate my htaccess file, but I think its hidden by default. I've tried to find it using a terminal and connecting to my site - also with no success.
Any help to solve this issue would be great cheers!
If /About/ is a directory and these are static HTML files, rename about.html to index.html.
Servers will look for index.html in a directory if nothing else was specified in the URL.

htaccess rewrite resulting in 500 Internal Server Error

I am trying to get pretty urls using htaccess for SLIR image resizing plugin.
I want to rewrite something like below:
Before Rewrite:
localhost/img/600x400/slider/image.jpg
After Rewrite
localhost/application/public/slir/index.php?r=slir&w=600&h=400&i=img/slider/image.jpg
But the following code is returning a 500 Internal server error...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/([0-9]{1,4})x([0-9]{1,4})/(.*)\.(gif|jpg|jpeg|png) application/public/slir/index.php?r=slir&w=$2&h=$3&i=$1/$4.$5 [NC,NE,QSA,L]
</IfModule>
Please help me.....
UPDATE
I thought it was a File Permissions issue...
so I checked all the related directories and files, surprisingly there were all 755/644.
Then I directly checked the /application/public/slir/index.php. It was not accessible.
I created another file named hello.php in the same slir directory and hello.php was accessible.
Then I moved the SLIR directory to the another folder named public. Here, both the index.php and hello.php were accessible.
After that, I moved SLIR directory to back to the original scripts folder. In scripts folder, hello.php is accessible but index.php is not accessible.
Now, Both files have same permissions and are in same folder.. I dont know what is happening here... :/ :/
Someone please help me......
In case anyone else is looking at this - the problem I found was these two lines in the htaccess file:
php_value auto_prepend_file none
&
php_value auto_append_file none
some servers don't allow them.
moving them to my php.ini file solved it for me:
auto-prepend-file = none
auto-append-file = none
That is probably because you are editing the .htaccess file with a Windows based editor like notepad. Notepad adds some characters to the file which you can't see, but they mess with the .htaccess. I recommend using an editor like Notepad++ and setting the mode to Unix, so it won't mess with the server..
Speaking technically that is because Linux and Windows have different type of EOL ( End Of Line ) so that notepad adds some characters to the end of every line to go to the next line, but Unix won't recognize them, because it has it's own type of EOL..
Slashes are normally forbidden in query strings, but you can enable them by setting
AllowEncodedSlashes directive.
You may also want your rewrite rule to use percent encoding for slashes (that is %2F) so after rewrite:
localhost/application/public/slir/index.php?r=slir&w=600&h=400&i=img%2Fslider%2Fimage.jpg
See also How do you configure apache php to accept slashes in query strings
Duplicate, phrased very differently:
Has anyone used Smart Image Resizer on subdomain sites?
The problem is the subdomain...

Recurse .htaccess into subdirectories?

On our live site, coded by other developers, we have a globals file on the root. It's auto-prepended to each requested PHP file. Great.
We're now trying to make a development server to mirror our live, for internal purposes. I'm looking in their global config for apache, and I see this:
php_value auto_prepend_file /home/site/SITE/globals.php
However, while making an .htaccess file, I try to call the file using the direct URL, (based on the root of the server) and I get this error:
Fatal error: Unknown: Failed opening required 'opt/lampp/htdocs/globals.php'
(include_path='.:/opt/lampp/htdocs/site/SITE/include') in Unknown on line 0
It seems as though it's trying to use the include path for an auto-prepend. I can confirm that if I put just globals.php, it will work in the index and files on the root of the server. Do you guys have any leads as to what the problem could be?
Replacing our config file with the live one is not an option.
auto_prepend_file is being used when you want to include php file into every php file.
there are two kind of value it can has:
**File name**: in this case you put only file name and php searches according site's document root (or include path).
*Path & file name*: in this case you put file name and the path to file.
so, if their auto_prepend_file has the value /home/site/SITE/globals.php, so php looks in that path! so if you want to add that file into your php files you have to edit that php directive.

deny from all in .htaccess not blocking file in the corresponding folder

I have an .htaccess file in the folder called folder and another file, called form.html in the same folder
When I try to reach the folder (by entering http://blablabla/folder/), it does block the access and I am getting an error 403 but when I enter the exact URL of the file http://blablabla/folder/form.html anf hit enter, I can access the file as easily as if I haven't put any .htaccess file.
Am I doing something wrong?
Am I missing something, should I use something like or
Sorry if the question is really basic...
.htaccess is a container for directives for your apache web server that apply to that directory and below only. What directives have you got in your .htaccess file?
The behaviour you outline above is how apache should behave with no .htaccess folder.
What is it that you are wanting to happen?

Resources