.htaccess 301 redirect after trailing slash - .htaccess

I know this has been asked so many times before but I coulnd't find this particular script.
So I've started a franchise, and I've moved my www.site.com to shop1.site.com (shop2.site.com also exists.)
Google seems confused by the new setup so this is what I need
If someone goes to a url containing a directory after the trailing slash, I need to redirected to the new site with a 301
If not, leave it be
For example:
www.site.com/info/ -> shop1.site.com/info/
www.site.com/help/ -> shop1.site.com/help/
www.site.com (leave as is)
So basically any url with a directory, redirect, otherwise leave it be
Update
here is my current htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php

This is what I needed. This redirects every page except for the root. Be warned, this also redirect's all traffic to HTTP and not HTTPS
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#Rewrite everything except root
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^.+$ http://shop1.site.com%{REQUEST_URI} [L,R=301]

Related

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 subdomain of one domain to a subdomain of another domain

I am trying to redirect the following (respectively):
http://sub.firstdomain.com/d/(all_files_and_folders)
http://sub.firstdomain.com/d2/(all_files_and_folders)
to
http://sub.seconddomain.com/d/(all_files_and_folders)
http://sub.seconddomain.com/d2/(all_files_and_folders)
There are other files and folders under the first subdomain that I do not want redirected. Previously this was working but now it seems like Go Daddy changed something and what I had is no longer working. Here it is:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.+) /dstats/count.php?statspage=$1 [L,NC,QSA]
RewriteRule ^(.+)\.deb$ /dstats/count.php?file=$1.deb [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(d2?)/(.*)$ http://sub.seconddomain.com/$1/$2 [L,R=301]
You can ignore the RewriteRule. It is working fine. I wanted to make sure I included the entire .htaccess file just in case. My issue is with RewriteCond it seems.
The following should work:
RewriteEngine On
Redirect 301 /d/ http://sub.seconddomain.com/d/
Redirect 301 /d2/ http://sub.seconddomain.com/d2/
The above should only redirect anything in the /d/ and /d2/ folder of the sub subdomain.
EDIT: Second try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]

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>

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

htaccess Redirect 301 problem .. all redirects with one string fail to redirect and 404

So I have moved a website and am trying to 301 redirect everything, which I do quite often so this is a weird problem but probably something stupid I'm not seeing.
ALL of my redirects are working fine, except any redirect that the first string starts with "/Dining" or "/dining" are failing. For example, this redirect works fine-
Redirect 301 /healthfitness/teeth.cfm /healthcare/pretty-teeth
...as well as 100s of others.
But all of these are failing (many more than I'm showing)-
Redirect 301 /Dining/diningreviews/vawines.cfm /shopping/wines-2004
Redirect 301 /Dining/diningathome/carrotcake.cfm /home-garden/carrot-cake-2003
Redirect 301 /Dining/diningathome/oldvarolls.cfm /home-garden/virginia-rolls-2003
Redirect 301 /Dining/diningathome/pumpkincake.cfm /home-garden/pumpkin-cake-2003
The top of my .htaccess file looks like this-
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
#Everything below here are Redirect 301s
Dont redirect statements have to include the protocol in the destination?
You must include the domain name in the redirect.
Never mix mod_alias and mod_rewrite directives in the same file. If you use RewriteRule for any of your rules, you must use RewriteRule (not Redirect or RedirectMatch) for ALL of your rules.
List all redirects (using RewriteRule syntax) before any of the rewrites (using RewriteRule syntax) otherwise you will inadvertently expose rewritten pointers back out on the the web as new URLs.
The code you are currently using is very very inefficient. The .* patterns mean your .htaccess file will attempt hundreds of thousands of "back off and retry" trial matches for every URL request hitting the server.
In particular,
^(.\*/)? should be replaced by ^([^/]+/)\* in two places.
^.\*/ should be replaced by ^/([^/]+/)\*
!.\*wp-content/plugins.\* should be replaced by !wp-content/plugins
^([_0-9a-zA-Z-]+/)?(.\*\\.php)$ should be replaced by ^([_0-9A-Z-]+/)?([^.]+\\.php)$ with the [NC] flag also added.
The "add a trailing slash to /wp-admin" redirect should be the very first ruleset in the file and the target URL should include the protocol and domain name. That rule should be immediately followed with a non-www to www canonicalisation rule.
The new code may well run hundreds of times quicker.

Resources