403 Access Denied in Apache2 website after updating .htaccess file - .htaccess

I am unable to access retrieve files hosted on my webserver called by my application. (403 forbidden).
My Application file is hosted at /var/www/mysite/program.application. If I browse to this specific directory (IE11), I can view the program.application, but when my program.application file tries to hit /var/www/mysite/files/dependency.application, I get a 403 error. This stopped working right after I implemented the rule to my .htaccess file. Removing this rule I do not have any 403.
drwxr-xr-x 3 www-data www-data 4.0K Sep 10 23:37 'files'
-rwxr-xr-x 1 www-data www-data 1.7K Sep 10 20:42 myprogram.application
-rwxr-xr-x 1 www-data www-data 90K Sep 10 20:56 index.html
Here is my current configuration in my .htaccess file
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!=Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
RewriteRule ^ - [F]

Related

modrewrite how can all files to index.php?

actual .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule index.php [L]
target:
All files should point to index.php
also png/xml ect.
call on browser:
http://localhost:8080/test.png
browser message:
enter image description here
log:
[Sun Feb 6 01:23:29 2022] [::1]:57052 Accepted
[Sun Feb 6 01:23:29 2022] [::1]:57052 [404]: GET /test.png - No such file or directory
[Sun Feb 6 01:23:29 2022] [::1]:57052 Closing
my server start to test with:
php -S localhost:8080 -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes
my actual server:
PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
works on macbook ...
have an idea to solve my problem?
PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS)
You are using the built-in PHP webserver. .htaccess (and by extension mod_rewrite) is an Apache config file and is not supported by the PHP webserver.
To implement a front-controller/router using the PHP built-in webserver you need to use PHP, as described in the PHP docs:
https://www.php.net/manual/en/features.commandline.webserver.php
Aside: The mod_rewrite rule is also incorrect and won't work as written:
RewriteRule index.php [L]
You are missing the first argument to the RewriteRule directive, so it will never match the request you are making. It should be something like this:
RewriteRule . index.php [L]
Where . (a single dot) is a regular expression (pattern) that matches at least 1 character (ie. everything except the document root, which is handled by the DirectoryIndex).
Further Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
works on MacBook ...
You must be using Apache, with a different .htaccess file!

Apache 403 error from accessing certain index.php or index.html despite having similar permissions for each

My apache server was once working, but randomly it started spitting out a 403 error for just about every directory. Two directories remain working, how can I get all directories within /srv/www/htdocs working correctly?
I have looked at the permissions for the two directories that work and compared them to other files/folders that aren't being accessed. The two working directories are "BIBETIS" and "nextcloud", while everything else does not work.
The index.php file under BIBETIS is owned by user root, and group www, its permissions are -rwxrwxr-x (I believe 755?). This directory loads fine and functions as expected.
The index.php and index.html file under nextcloud is owned by user wwwrun and group www. Its permissions are -rwxrwxr-x once again. This all functions as expected.
So, based off this I have tried doing sudo chown -R wwwrun:www /srv/www/htdocs, sudo chown -R wwwrun:www /srv/www/htdocs/*, chmod -R 755 /srv/www/htdocs/ and similar variations. This did not fix the 403 error in apache.
I decided to take a step back and focus on a single, more simple directory: "map". This folder has one index.html file. I once again tried changing the permissions as described above, yet nothing seems to help.
The error log for apache reads:
[Fri May 10 01:39:35.665344 2019] [autoindex:error] [pid 2943] [client -:50203] AH01276: Cannot serve directory /srv/www/htdocs/map/: No matching DirectoryIndex (index.php4,index.php5,index.php) found, and server-generated directory index forbidden by Options directive
[Fri May 10 01:39:36.744342 2019] [autoindex:error] [pid 2943] [client -:50203] AH01276: Cannot serve directory /srv/www/htdocs/map/: No matching DirectoryIndex (index.php4,index.php5,index.php) found, and server-generated directory index forbidden by Options directive
[Fri May 10 01:39:42.323373 2019] [autoindex:error] [pid 2944] [client -:50204] AH01276: Cannot serve directory /srv/www/htdocs/map/: No matching DirectoryIndex (index.php4,index.php5,index.php) found, and server-generated directory index forbidden by Options directive
[Fri May 10 01:42:10.018496 2019] [autoindex:error] [pid 2038] [client -:50879] AH01276: Cannot serve directory /srv/www/htdocs/: No matching DirectoryIndex (index.php4,index.php5,index.php) found, and server-generated directory index forbidden by Options directive
Which led me to believe it is only searching for index.php files? So I set up an index.php under the map directory, applied correct permissions, and it worked. If so, why can it not access the main index.php located within the htdocs directory even after setting proper user:group and 755 permissions?
Can you show us the configuration of your vhost and your apache.conf/httpd.conf ?
To be able to read the index.html you need to add to the directive DirectoryIndex index.html otherwise your web server won't know that he needs to read it, it should look something like this:
DirectoryIndex index.html index.php4 index.php5 index.php
As the error message shows clearly, it's not related to file/folder permissions!

Configuring Apache for localhost

I am new to Linux and Apache. I have tried to configure Apache2 to serve a website I develop on my local Linux Mint machine. I have not attepted to use PHP yet.
In spite of trying to follow all the instructions, I still got a 404 Not Found or 403 Forbidden.
The name of the directory that holds the website is Web and has permissions
drwxr-xr-x 4 www-data www-data 4096 May 15 18:46 Web
I also tried my own user group with no luck. All files inside Web have the usual r-x or r-- permissions for both Group and Others.
/etc/hosts is
127.0.0.1 localhost
/etc/apache2/ports.conf is
Listen 127.0.0.1:80
/etc/apache2/sites-available/Web.conf is
<VirtualHost 127.0.0.1:80>
ServerName Web
ServerAdmin webmaster#localhost
DocumentRoot /xxx/xxxx
</VirtualHost>
Note that /xxx is my custom partion on my local machine and xxxx is a directory in this partion.
/etc/apache2/sites-enabled is
lrwxrwxrwx 1 root root 27 May 23 13:04 Web.conf -> ../sites-available/Web.conf
Finally, the /etc/apache2/apache2.conf included this
<Directory /xxx/xxxx/Web/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
I finally did get localhost to work, but it was not as above. I simply made a link to the directory Web in /var/www/html.
/var/www/html is
-rw-r--r-- 1 root root 11321 Apr 14 15:11 index.html
lrwxrwxrwx 1 root root 17 May 22 16:54 Web -> /xxx/xxxx/Web
This worked with no chances in apache2.conf and I did not have to enable the site (make a link with a2ensite).
The url
localhost/Web
now works with no errors.
It works, but it can not be the right way. What am I doing wrong?
/xxx/xxxx also needs to be readable by www-data in order to work as document root. You'll also need the permissions defined in a directory block.
<Directory /xxx/xxxx/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Or set your DocumentRoot /xxx/xxxx/Web

Problems with .htaccess after upgrading to PHP 5.4

I have had some difficulties with my site after updating my servers to use PHP version 5.4.. I have been through a lot of support tickets and browsed the internet for 2 days.. Now I need to ask directly to the people who actually know about it.
After updating the php version I started to get a 500 error on my pages and it turned out that my rewrite rules in my htaccess file wasn't working anylonger. They did in php version 5.2 but not in 5.4.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+website\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([\w.-]+)/?$ website.php?id=$1 [L,QSA]
RewriteRule ^([\w.-]+)/([\w.-]+)/?$ website.php?id=$1&page=$2 [L,QSA]
</IfModule>
Can anybody tell me what I have to do differently? I would really appreciate it.
EDIT:
I got this copy from my livechat with hostgator:
[Thu Jun 26 03:54:07 2014] [error] [client ] malformed header from script. Bad header=<br />: website.php
[Thu Jun 26 03:55:35 2014] [error] [client ] Cannot load the ionCube PHP Loader - it was built with configuration 2.2.0, whereas running engine is API220100525,NTS
[Thu Jun 26 03:55:35 2014] [error] [client ] The Zend Engine API version 220100525 which is installed, is newer.
[Thu Jun 26 03:55:35 2014] [error] [client ] Contact Zend Technologies at http://www.zend.com/ for a later version of Zend Optimizer.
[Thu Jun 26 03:55:35 2014] [error] [client ]
Try adding this line:
zend_extension = /usr/local/ioncube/ioncube_loader_lin_5.4.so
to your php.ini
SOLUTION IS NOW FOUND
I made the wrong assumption to think it was due to my rewrite rule. The problem seemed to be at HostGator. If you host at HostGator, this will be helpful for you. The reason all this started was because HostGator has announced, that the will upgrade the servers to run PHP 5.4 as standard instead of PHP 5.2. What they didn't mention, was the structure of their servers and how individual sites has to manage their individual PHP files.
Well I found out that by changing the name of my php.ini file in the root directory (making it invalid for the server to use), the system located a PHP version specific php.ini file in the main servers directory. This made me realize that the php.ini file in my root directory had to be changed completely, so therefor I created a new php.ini file containing the 5.4 version. The site didn't change, because the files were basically identical, but it made it possible for me to use the EZConfig in cPanel again.
This is what to do:
Change PHP version either in the PHP configuration tab in cPanel, or
by adding AddHandler application/x-httpd-php54 .php to your
.htaccess file.
Open File Manager in cPanel.
Locate your root folder (Home) and find the php.ini file.
If you want to keep your existing php.ini file as a backup, rename it to ex. php_old.ini.
Create a new file named php.ini and insert the content from following link: https://wiki.cac.washington.edu/display/webpub/php.ini+for+PHP+5.4
Good luck with it. Hope this will solve it for somebody as well, so that they don't have to spend 3 days solving it themselves.

Redirect works fine but I get File does not exist in logs

I have this .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^user/([_.0-9a-zA-Z]+)$ show-user.php?user=$1 [L]
which redirects just fine in the browser, but if on the error log on my server I can see hundreds of "File does not exist" errors, do you have any idea why?
Here is an error example from the log:
[Thu Mar 28 02:58:43 2013] [error] [client xxx.xxx.xxx.xxx] File does not exist: /home/folder/public_html/user, referer: domain/user/username0001
And there are hundreds! But everything works fine! What can I do to make these errors not show up?
Fixed, I had to use /show-user.php like this
RewriteRule ^user/([_.0-9a-zA-Z]+)$ /show-user.php?user=$1 [L]

Resources