.htaccess redirection problems - .htaccess

I am creating a website using 000webhost.com. This is set up so that mysite.com goes to www.mysite.com, and mysite.com/page goes to mysite.com/under_construction/page. A few problems:
mysite.com goes to www.mysite.com/under_construction instead of www.mysite.com (although www.mysite.com does show the contents of under_construction)
mysite.com/invalidpage goes to the default error page instead of error.mysite.com/404.
error.mysite.com shows up just fine, but www.error.mysite.com brings me to the default error page.
I have the following DNS zone record set up:
www.bossgamerz.cu.cc CNAME bossgamerz.cu.cc
RewriteEngine On
#Options +FollowSymLinks
ErrorDocument 404 http://www.error.bossgamerz.cu.cc/404
ErrorDocument 403 http://www.error.bossgamerz.cu.cc/403
RewriteBase /
RewriteCond %{HTTP_HOST} www.bossgamerz.cu.cc
RewriteCond %{REQUEST_URI} !under_construction/
RewriteRule ^(.*)$ under_construction/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.bossgamerz.cu.cc/$1 [R=301,L]
I do not neccesarily need the error messages to be in an error subdomain.

Your rules seem to be not in right order and you need to exclude error pages from rewrites. Try this code:
Options +FollowSymLinks
ErrorDocument 404 /404
ErrorDocument 403 /403
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.bossgamerz.cu.cc%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/(under_construction|404|403)
RewriteRule ^ under_construction [L]

Related

Redirect url contains both numbers and letters with Htaccess

Im trying to Rewrite my url with this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
ErrorDocument 404 mysite
RewriteRule ^([a-z-]+)$ /name.php?n=$1 [NC]
RewriteRule ^([0-9]+)$ /age.php?e=$1 [NC]
</IfModule>
when i tried open link mysite/abc or mysite/20. Both of them are working but if try this mysite/abc123 or mysite.com/a2b2c the page show error message. This page isn’t working
How can i redirect those link mysite/abc123 , mysite/a2b2c to my home page mysite with htaccess ?
Thank you!
You may try these rules:
ErrorDocument 404 /
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^([a-z-]+)/?$ name.php?n=$1 [L,QSA,NC]
RewriteRule ^([0-9]+)/?$ age.php?e=$1 [L,QSA,NC]
ErrorDocument 404 / will forward all not found URLs to home page without changing the URL in browser. If you want to redirect those 404 URLs and want to change them to home (landing) page then use:
ErrorDocument 404 https://example.com/

htaccess rewrite 301 redirect rule without www

I have a client who has a rewrite rule on their website that uses 404 and SSL with https: but it always rewrites to 'www.mywebsite.com' . They want it without the www. whenever I try to modify the rule the code breaks with 'too many redirects'
Here's the old .htaccess rule
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://www.mywebsite.com
I've tried to remove the 'www' above but when I do that, you get an error - endless redirects.
The goal is a simple rewrite rule that allows all redirects and 404 errors to go to https://mywebsite.com with a 301
My engineer found a solution so I'm posting it here.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [R,L]
ErrorDocument 404 http://mywebsite.com
Let me explain you previous rules :
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://www.mywebsite.com
This line RewriteCond %{HTTP_HOST} . is matching any request start with any character except newline.
This line RewriteCond %{HTTP_HOST} !^www\. [NC] is matching any request not start with www.
This line RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] is to force what satisy the previous conditions , which is only request without www, and fore it into https + wwww and that why you faced that problem .
The new rules :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [R,L]
ErrorDocument 404 http://mywebsite.com
these in general will match http requests by this RewriteCond %{SERVER_PORT} 80 and force them into https without www.
But that will not force https://www into https:// so change them to :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [R,L]
ErrorDocument 404 http://mywebsite.com
If this code is Ok , change [R,L] to [R=301,L] to be permenant redirection because R flag alone means 302 which is temporary .

404 issue in two htaccess in root domain and subfoler

My root domain htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteCond %{`HTTP_HOST`} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ `https://%1/$1` [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) `https://%{HTTP_HOST}%{REQUEST_URI}` [R=301,L]
In my subfolder htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]
Wehn I type in example.com/any-wrong-url, it will redirect to 404 page, that is correct.
But when I type in example.com/subfoler/any-wrong-url it can not redirect to 404 page. Where do I something wrong?
The best way to do this without creating any conflict or redirect is:
Use /subfolder/ instead of root / on the RewriteBase like this:
RewriteBase /subfolder/
Copy the 404.php inside the subfolder.
The complete code will look like:
RewriteEngine On
RewriteBase /subfolder/
ErrorDocument 404 /404.php
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]
This should work.

Redirection works BUT

I have set up a redirect from old domain to new domain and it works however there are links which is not redirecting properly....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.us$
RewriteRule (.*)$ http://www.new.com/$1 [R=301,L]
</IfModule>
I am trying to redirect : www.old.us/scripts/affiliate.pl?id=505 to www.new.com
but redirects like this : www.new.com/scripts/affiliate.pl?id=505 results a 404 page.
Change your code to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old\.us$ [NC]
RewriteRule ^ http://www.new.com/? [R=301,L]

.htaccess redirect subdomain to main site - no exceptions

I'm trying to redirect all requests for subdomain.example.com to www.example.com
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Seems to cause a 500 internal server error as does
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
So my question is why are these failing and how do I fix it?
After your first two lines, add:
RewriteCond %{http_host} ^.domain.com
RewriteRule ^(.*) http://domain.com/$1 [R=301]

Resources