.htaccess files not working in php website - .htaccess

I have .htaccess file with this below code that time website showing with one error message 500 internal server error.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule (.*) http://www.website.com/$1 [L,R=301]
When I renamed to .htaccess file that time it not showing any error it is working fine. I am using 1and1 server windows control panel.
I given file permissions to folders '0755' and for files '0644' and for .htaccess file '604'.
where I did mistake. please suggest me.

Related

htaccess file - root and website folder htaccess file both have different code. URL does not work without the www

I have the following domain that does not work without the www.
wwww.mydomain.com works
mydomain.com = does not work
The root htaccess file (primary domain is something else) automatically seemed to have this code in it.
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.io\/mydomain" [R=301,L]
I added this code in the website's own htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What am I doing wrong?
Currently mydomain.com goes to something quite strange (it duplicates the domain name): and has an error
https://www.example.com/example.com/
Error on page
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Can I delete the code which says "do not edit". Which one is working and why/how? A fix with an explanation would be appreciated.
The issue was basically resolved by:
Adding the rewrite engine rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Deleting the forwarding in the DNS management console and ONLY leaving in the forwarding in the addondomains section. So, in the cpanel - addon domains, create forwarding and delete the forwarding in the DNS section. This was the key part.
There was an additional folder outside of the public_html for this domain and that was deleted as well.
Finally, adding these rules to force https was useful:
https://www.hostinger.co.uk/tutorials/ssl/forcing-https
Also noting the helpful comment: "Which web server are you using? Look through your web server configuration files. Your problem is that you have a redirect from HTTP to HTTPS and the path URL is configured wrong."

htaccess rewrite rule not working (AllowOverride on)

After reading several questions/answer on stackOverflow, I can't find why my htaccess rule isn't working.
This is the rule :
RewriteEngine On
RewriteRule login$ login.php [NC]
In my home folder I have the file login.php but when I call http://myserverip/login I get a 404.
When I call http://myserverip/login.php the page is displayed.
When I put anything in my htaccess I receive a internal server error which confirms my htaccess file is used.
So where the problem can be ?

.htaccess redirect in folder on domain affects same named folder in subdomain

I recently changed a site I've developed to use a Wildcard SSL certificate but I'm having an issue with .htaccess ReWrites in the domain.com/projects folder affecting the cms.domain.com/projects folder.
In the www.domain.com/projects folder is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^([^\.]+)$ $1 [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ /projects/project.php?id=$1 [L]
When an SEO friendly project url e.g. www.domain.com/projects/project-title gets sent to this folder it is directed to the project.php page where it can be displayed.
This works fine, but I came across a problem in the subdomain cms.domain.com/projects, this folder and all its contents display a 404 error, even though all the files are there. I know this .htaccess file is the issue because I have changed the cms.domain/projects folder name and the sub directories appear and I've also removed the code from the .htaccess file and the cms.domain.com/projects folder appears.
I have tried, many of the suggestions found here and else where adding RewriteCond %{HTTP_HOST} ^www.domain.com$ and denying the cms.domain.com url but nothing works, and the projects folder in the cms remains stubbornly in 404 land. I've been looking at this for several days and have found nothing similar on the Interweb. Any help I can get would be greatly appreciated.
Thank you for all the replies so far.

.htaccess rewrite and subdomains connected to subdirectories

I have installed wordpress to my public_html/ folder on my host. Now, I want to help my friend to get his own wp blog. I wanted to add a folder public_html/zoran/ where I wanted to install another wp and to add subdomain zoran.example.com to call that folder.
Problem is that .htaccess from public_html/ (my wp installation) is messing .htaccess inside public_html/zoran/ folder. I googled a lot, and figured out that this is impossible to have one .htaccess rules in root folder and other in subfolder (mod rewrite).
I cannot access my cPanel for couple of days now, and it will be unavailable for a few days, I recieved mail about that.
Now, I am at this point. I have moved my wp into public_html/www/ and I stored .htaccess file into public_html/ with following content:
# Rewriting conditions
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.example.com/ [R,L]
# Identify subdomain as %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
# Check if %1 is directory
RewriteCond %1 !-d
# rewrite the request to the subdomain's subfolder
RewriteRule (.*) %1/$1 [L]
Above code redirects example.com to www.example.com, and also, it opens my wp, stored in public_html/www/. But, when I try to go to zoran.example.com, I get 500 error, and that folder has just index.html file in it.
What is happening? How come that www folder works, and folder zoran does not work on same principle?!

RewriteCond to skip actual directories seems to be ignored

My problem is that The RewriteRule is still matching when I visit a physical directory, e.g. http://a-domain.com/foo/ where foo is a normal directory in the web root.
the .htaccess file has:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .? index.php [L]
It works fine on my dev server but not on a live WHM/CPanel server. I'm a bit lost.
I've tested this on my cpanel website and you're right and I found out where's the problem. when you try to access a folder in your website that doesn't have default file (index) , it tries to access the file that is responsible for 403 HTTP code, and because that doesn't exist, it rewrites URL to index.php.
All you have to do is add this to above of your .htaccess file:
ErrorDocument 403 /index.php?type=err&code=403
I tried this on a VM which mirrors my own shared service and it works fine for me. Have you got another .htaccess in the foo directory? If so, then the rewrite engine will ignore your one in DOCROOT and use this instead?
The /foo/ dir has basic authentication so it was failing when %{REQUEST_FILENAME} contained a non-existent file /home/the_user/public_html/401.shtml. I didn't think this was an issue because the basic authentication worked, and when I cancelled the auth prompt I was being served a standard 401 file. Usually when those ErrorDocument files are missing, Apache says Additionally, an error of type 404 was encountered while trying to use an ErrorDocument to handle the request.

Resources