Error document configured in .htaccess file not work properly - apache

On my cPanel account I have a htaccess file which looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)\.html$ $1.php [nc] //for rewrite all .php file with .html extension
RewriteRule ^$ /index.html [R=301,L] //for rewrite home page to url with index.html
RewriteRule ^/$ /index.html [R=301,L] //for rewrite home page to url with index.html
ErrorDocument 404 /404.html
Problem is: when i open link like:
https://www.domainname.tld/test ---> i have correct 404 error (page not exist)
https://www.domainname.tld/test.php or test.html ---> i have default 404 error of Apache
This is very strange...
I contact cPanel support and they say me that not is a cPanel error
Can you help me?

Problem was not with code inside htaccess but a config of cPanel! I disbale from WHM PHP-FM for interested domain (read here how to disable https://support.cpanel.net/hc/en-us/articles/360049690354-How-to-Disable-PHP-FPM) because staff of cPanel explain me that was error. (read here https://support.cpanel.net/hc/en-us/articles/360050895654-ErrorDocument-directive-not-being-honored-when-using-PHP-FPM-and-the-filename-provided-has-a-php-extension)

Related

How to fix not found error when redirect to https://www is used with RewriteBase?

I want any access to my website to redirect to https://www. but I have a RewriteBase.
Suppose my website is example.com, I want:
example.com to redirect to https://www.example.com
http://example.com to redirect to https://www.example.com
https://example.com to redirect to https://www.example.com
https://www.example.com to redirect to https://www.example.com
And my website files are located in a subfolder, so I have a RewriteBase which is for example:
RewriteBase /~myuser/mysite
My .htaccess file contains:
RewriteEngine On
RewriteBase /~myuser/mysite
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I referred to the following question to write my .htaccess file: htaccess redirect to https://www
As a result, if I write on in a browser a url such as example.com/page1 (which is a valid page), it redirects to https://www.example.com/page1 however, I get the following error:
Not Found
The requested URL /page1 was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I think the problem is caused by the RewriteBase, but I do not know how to fix it.
Note that when I access the website main url https://www.example.com all internal sub-links work correctly.
Here is the solution
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /~myuser/mysite
Simply I placed the RewriteBase at the end after the RewriteRule and now it works.

Apache URL Rewriting - Hide path in URL only when accessing specific path

I would like to rewrite my URL only when typing this:
www.domain.com/dir1/dir2/dir3/
in that path there is a index.html file that will be automatically loaded. And I'd like it to become this:
www.domain.com/index.html
It is important that the root www.domain.com does not redirect to that subdirs because it already redirects to another .php file.
I wrote the following .htaccess file:
RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]
but it redirects even when typing the root domain.com.
Could you help me with that?
Edit:
I have tried in another browser and the real problem is that when typing domain.com/dir1/dir2/dir3 or domain.com/dir1/dir2/dir3/index.html the dir1/dir2/dir3/ is still shown and is not hidden.
Edit 2:
my complete .htaccess:
AddHandler application/x-httpd-php56s .php
ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]
Thanks
Replace your current code with this:
DirectorySlash On
RewriteEngine On
# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]

Index redirects me to a different file

Wasnt sure what to put in the title to describe what is going on, but I was looking at ways I could deny direct access to files and folders. I added a line in .htaccess which didnt work so I removed it.
I have access to every page on my website, but index redirects me to the page I tried to prevent direct access to. I have deleted the content of .htaccess, index and the page im been redirected to, restarted wamp and used CTRL + F5 but nothing seem to work. No cookies are stored and no errors in inspect element.
This is my .htaccess file
Options -MultiViews +FollowSymlinks
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^http://example.com$ [NC]
#ERROR CODE -----------------------------------------------------------
ErrorDocument 400 /assets/error/400.php
ErrorDocument 401 /assets/error/401.php
ErrorDocument 403 /assets/error/403.php
ErrorDocument 404 /assets/error/404.php
ErrorDocument 500 /assets/error/500.php
RewriteRule ^400/?$ /assets/error/400.php [NC,L]
RewriteRule ^401/?$ /assets/error/401.php [NC,L]
RewriteRule ^403/?$ /assets/error/403.php [NC,L]
RewriteRule ^404/?$ /assets/error/404.php [NC,L]
RewriteRule ^500/?$ /assets/error/500.php [NC,L]
#User Pages-------------------------------------------------------
RewriteRule ^//?$ /index.php [NC,L]
RewriteRule ^signin/?$ /signin.php [NC,L]
RewriteRule ^signup/?$ /signup.php [NC,L]
RewriteRule ^signout/?$ /signout.php [NC,L]
RewriteRule ^forgot-password/?$ /forgot-password.php [NC,L]
RewriteRule ^credits/?$ /credits.php [NC,L]
RewriteRule ^leaderboard/?$ /leaderboard.php [NC,L]
RewriteRule ^updates/?$ /updates.php [NC,L]
RewriteRule ^live/?$ /live.php [NC,L]
#Misc--------------------------------------------------------------
RewriteRule ^misc/?$ /misc.php [NC,L]
RewriteRule ^maintenance/?$ /maintenance.php [NC,L]
#Maintenance-------------------------------------------------------
# files to load
# RewriteCond %{REQUEST_URI} !/assets/css/main.css$
# RewriteCond %{REQUEST_URI} !/assets/css/special.css$
# RewriteCond %{REQUEST_URI} !/maintenance.php$
# RewriteCond %{REQUEST_URI} !/.htaccess$
# here filter the developer's IP/
# RewriteCond %{REMOTE_HOST} !^212.251.187.11
# redirect page (maintenance page)
# RewriteRule $ /maintenance [R=302,L]
Edit
The problem seem to be occurring when I use example.com or example.com/, but not when using example.com/index.php.
There is only 1 .htaccess file been used and I've not made any changes to the apache files (atleast not my self), Im trying to find the .htaccess snippets I tried right before this started happening
I transfered my root folder to a different pc and everything works. I'm not sure what went wrong but I was not able to fix the problem. I'm reinstalling wamp to see if that solves the problem. I belive this is the code that I used before index stopped working Redirect 301 /old/file.html http://yourdomain.com/new/file.html I never replaced any of it with the index but with a 404 page and the page I was been redirected to.
The snippet was taken from:
http://viralpatel.net/blogs/21-very-useful-htaccess-tips-tricks/

301 Redirect for specific url

I want to edit the URL of a page (only one page called meto.php) to a custom URL.
It is important that the meto.php file located in sub-directory /pob/
i want to change this www.domain/pob/meto.php to www.domain/pob/define.php
When visitor type this address www.domain/pob/meto.php address bar show this address: www.domain/pob/define.php
Please note define.php file does not exist.
I tried this code in .htaccess file at /pob/ (not root directory):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [r=301,nc]
Redirect 301 meto.php define.php
//also
Redirect 301 /meto.php http://www.domain.com/pob/define.php
//
RewriteRule ^meto.php define.php
Unfortunately all of the above tries fails.
Server info:
Apache Version 2.2
PHP: 5.5
You can not rewrite your files using Redirect Directiv of mod_alias , it is used for external redirection.
Try mod_rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/pob/$1 [R=301,L]
#1) redirect "/pob/meto.php to "/pob/define.php"
RewriteCond %{THE_REQUEST} /meto.php [NC]
RewriteRule ^ /define.php [NC,L,R]
#2) rewrite "pob/define.php" to "/pob/meto.php"
RewriteRule ^define\.php$ /pob/meto.php [NC,L]

.htaccess - Redirect to a subfolder according to the preferred language

we have a website in English. Now we want to have a translated copy of this website in German.
The index.html is located in the root folder: www.mydomain.com/index.html
Now I want to create a htaccess file which redirects all user which have set the preferred language in their browser to German (de, de-de, de-at, de-ch) to www.mydomain.com/de/index.html. All other user should be redirected to www.mydomain.com/en/index.html.
I tried a lot but I can't make it work. I do not really understand the regular expression thing and it's usage.
Most of the tries ended up with error 403 or 500. The current htaccess doesn't end up in an error, but it doesn't work.
The htaccess is now:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/(.*)$ /de/$1 [L]
RewriteRule ^/(.*)$ /en/$1 [L]
It seems like the file doesn't work at all. When I open www.mydomain.com it opens www.mydomain.com/index.html
Htaccess and rewriting works in general:
RewriteRule ^(.*)$ http://www.google.com/ [R=301,L]
works.
If I delete the slash behind ^
RewriteRule ^(.*)$ /en/$1 [L]
I get an error 500.
What do I do wrong?
I actually want the server to redirect to the webpage which the user tried to open. So if the user opens mydomain.com/index2.html he should be redirected to mydomain.com/xx/index2.html (and not to index.html)
Any ideas?
Thanks,
Jaroslaw
You can use this rule:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]