Redirecting subdomain and subfolder to root domain - .htaccess

I think I'am having problems with SEO because I'am using 2 domains in the same server.
In the root folder I have www.dinastiabus.pt and in root/viaescola.pt I have www.viaescola.pt.
Dinastiabus is well indexed by google but Viaescola is not.
What I think I need to do is to redirect with .htaccess:
viaescola.dinastiabus.pt to viaescola.pt
and dinastiabus.pt/viaescola.pt/ to viaescola.pt
Even if it's not the problem I would like to do it anyway.
I also would like the link to always have www.
This is what I have at this moment (but although it doesn't even have everything I want it is simply not working!):
RewriteEngine on
# Redirect to domain to www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
RedirectMatch 301 ^/www\.dinastiabus\.pt/viaescola\.pt/(.*)$ /www.viaescola.pt/$1
RedirectMatch 301 ^/dinastiabus\.pt/viaescola\.pt/(.*)$ /www.viaescola.pt/$1
# Prevent viewing of htaccess file.
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Prevent directory listings
Options All -Indexes

These two RewriteRules should work:
RewriteCond %{HTTP_HOST} ^(www\.)?viaescola\.dinastiabus\.pt$ [NC]
RewriteRule ^ http://www.viaescola.pt%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?dinastiabus.pt$ [NC]
RewriteRule ^viaescola.pt(.*) http://www.viaescola.pt$1 [R=301]
This will take any URLs starting with
http://viaescola.dinastiabus.pt
http://www.viaescola.dinastiabus.pt/
http://dinastiabus.pt/viaescola.pt/
http://www.dinastiabus.pt/viaescola.pt/
and convert them to
http://www.viaescola.pt

Related

Redirect http to https and www to non-www in .htaccess

First of all, I know there are lots of answers on this, but I don't actually find one that works. This is what I have in the .htaccess file right now, and I want to mention that it worked previously, but it does not anymore.
Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
# FORCE HTTPS AND NON WWW
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As a mention, I will have a lot of Redirect 301 from old pages to the new ones since the entire structure has been changed.
And the links that I am redirecting inside my website come with "www" like:
https://www.example.com/unt-de-cacao-de-plaja/filtre/producator/crisnatur/
and needs to be redirected to:
https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected
to the https/non-www address
OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
A few issues, in order of importance:
You have your canonical HTTP to HTTPS and www to non-www redirects at the end of the file. By placing it at the end of the file, after your front-controller, it's simply never going to be processed for most requests. This needs to be near the start of the .htaccess file, before your front-controller.
You should avoid mixing redirects from both mod_alias (Redirect) and mod_rewrite (RewriteRule) in the same scope. Different modules execute at different times throughout the request, despite their apparent order in the config file. Since mod_rewrite is required for other redirects, you should convert the mod_alias Redirect directives to use RewriteRule instead.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
You should include the canonical scheme and hostname in your URL redirects in order to avoid multiple redirects when requesting an "old" URL at a non-canonical scheme ot hostname.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
Depending on what you mean exactly by "a lot of Redirect 301" - you should not be doing this at all in .htaccess and instead redirecting in your server-side script, once you have determined that the request will 404. This is to prioritise normal site visiters and not your redirects (that get executed on every single request).
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Since you stated that these directives worked previously then I assume the use of the HTTPS environment variable is OK on your system. But note that, whilst this is relatively common, it's non-standard. (It implies the server is using some kind of SSL front-end/proxy.)
Note that the order of these rules will result in a double redirect when requesting http://www.example.com/<anything> (HTTP + www). Which is necessary if you are implementing HSTS, but otherwise, you should reverse these two rules to avoid this unnecessary double redirect.

301 Redirect Entire Domain Order of Commands Issue

I have yet another Domain 301 Redirect Question.
The .htaccess file is below. When I perform the full domain redirect, should the domain redirect fall before or after the redirects I currently have on the website due to some poor planning.
Long story short, I had a weebly site, I exported that and rebuilt it from scratch (it was a nightmare). Now I am just switching to a more SEO friendly domain name. When I moved it out, I also decided to change the URLs of many of the pages, so I initially had a lot of 404s and pages not being indexed.
.htaccess
DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
Header append Vary User-Agent
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old.com
RewriteRule ^(.*)$ http://www.example-old.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^2/(.*)
RewriteRule /index.html [R=301,L]
RewriteCond %{HTTP_HOST} ^1/(.*)
RewriteRule /index.html [R=301,L]
RewriteBase /
RewriteCond %{HTTP_HOST} !example-new.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [L,R=301]}
should the domain redirect fall before or after the redirects I currently have
The "full domain redirect", and by that I assume you mean the redirect from example-old.com to example-new.com, should be at the top of your redirects.
I assume example-old.com and example-new.com currently resolve to the same site - the new site. In which case you can combine your domain and www canonical redirects. For example, replace the following:
RewriteCond %{HTTP_HOST} ^example-old.com
RewriteRule ^(.*)$ http://www.example-old.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{HTTP_HOST} !example-new.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [L,R=301]}
With this:
RewriteCond %{HTTP_HOST} !=www.example-new.com [NC]
RewriteRule (.*) http://www.example-new.com/$1 [R=301,L]
This should go at the top of your mod_rewrite directives, immediately after the RewriteEngine On directive. This basically says, if the requested host is not www.example-new.com then redirect to www.example-new.com. So, this will catch example-old.com, www.example-old.com and example-new.com and anything else that isn't www.example-new.com!
Incidentally, you had an erroneous } (curly brace) at the end of the RewriteRule and the RewriteBase directive is not required here.
RewriteCond %{HTTP_HOST} ^2/(.*)
RewriteRule /index.html [R=301,L]
RewriteCond %{HTTP_HOST} ^1/(.*)
RewriteRule /index.html [R=301,L]
These two rules are invalid and don't actually do anything - or at least they don't do what they should be doing! The substitution (target URL) is missing from both the RewriteRule directives. But the RewriteCond directives also don't make sense and will never match anyway.
Also, my sites are indexing two different pages for root and /index.html.
Presumably you don't want the /index.html indexed and are referencing only / in your URLs? You can try adding the following redirect following the redirect above to help resolve this:
RewriteCond %{THE_REQUEST} /index\.html
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
This obviously assumes that index.html is your only DirectoryIndex (the file that Apache tries to return when you request a directory).
DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm
If you only have index.html index documents then you don't need to specify all the others that are not used. Incidentally, this list of files are in order to preference. Apache will first look for index.php, then index.php3, etc.

Redirect oldsite.com to new.oldsite.com, and sub.oldsite.com to new.oldsite.com/sub/

I have a situation where we've redesigned a site and have moved it to a new domain. The old site has a few client subdomains that we need to redirect to the new domain, but to folders that we will explicitly specify. Note that the new domain is actually a subdomain of the old domain, so not sure how that complicates anything, if at all.
For example, oldsite.com currently redirects all request to new.oldsite.com with the following rule:
RewriteCond %{HTTP_HOST} !^\.oldsite\.com
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
I also want to redirect some specific subdomains to folders of new.oldtsite.com, like subdomain.oldsite.com to new.oldsite.com/subdomain/. There also the likelihood that the old subdomain and the new directory will have different names (subdomain.oldsite.com to new.oldsite.com/subdomain-name/. :-/
I've attempted to duplicate this rule for the subdomain to folder redirect, but that just hijacks all redirects.
I will give you a virtual high-five if you can spot me a fix.
Have your rules from specific to generic. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/subdomain-name/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
EDIT: To redirect any.oldsite.com/foo to new.oldsite.com/any/foo
RewriteCond %{HTTP_HOST} ^([^.]+)\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/%1/$1 [R=302,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

how to redirect all www traffic with htaccess to equal non-www pages

Question is pretty simple I guess, but I fail to do this myself.
I have a website on http://website.com and want to 301 redirect ALL requests on www.website.com to http://website.com/ but make sure all directories and filenames stay the same (so not all requests end up at the homepage).
So whatever people may type (like www.website.com/something.here) should 301 redirect to website.com/something.here
How to do this?
Add the below to your .htaccess file. Should do the trick.
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
This requires that the rewrite engine is on...
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
</IfModule>

Htaccess 301 redirect for domain only

I have a list of 301 Redirects like this inside htaccess:
Redirect 301 /oldpage1.php http://www.domain.com/newpage1.php
Redirect 301 /oldpage2.php http://www.domain.com/newpage2.php
Redirect 301 /oldpage3.php http://www.domain.com/newpage3.php
...
Now these pages shall be redirected only for a certain domain (there are other domains now pointing to the same location like www.domain.com).
E.g. www.domain.com/oldpage1.php shall be redirected, but www.sub.domain.com/oldpage1.php not.
So whats the way to apply these redirects only for www.domain.com?
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage1\.php/?$ newpage1.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage2\.php/?$ newpage2.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage3\.php/?$ newpage3.php [L,R=301]
PS: If you have several rules like above then it is much better to use RewriteMap.

Resources