How can I redirect all requests going to web root to another folder (e.g. public/)?
I've already tried this (contents of .htaccess in web root):
RewriteEngine on
RewriteRule ^(.*)$ public/$1
But now I have duplicate content for addresses:
address.tld/ and address.tld/public/
I would like to redirect address.tld/public/ to address.tld/, so there won't be any duplicates, but I just don't know how to do it and not get into redirecting cycle...
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /public/
RewriteRule ^public/(.*) /$1 [L,R=301]
RewriteRule !^public/ public%{REQUEST_URI} [L]
With mod_rewrite you'll not get redirecting cycles.
Related
I am really confused... I have searched and experimented, but can't seem to get anything to work. So I gave up and asked here :)
How can I redirect /subfolder to root, but every page inside the subfolder should be redirected to root?
/subfolder redirects to root
/subfolder/another-folder redirects to root (not root/another-folder)
Thanks.
To redirect requests from /subfolder to / (to the root folder) ,you can use the following Rule in your htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} /subfolder/(.*)\sHTTP [NC]
RewriteRule ^.+$ /%1 [NE,L,R]
The RewriteRule above will redirect any request from /subfolder/foobar to /foobar but you will get a 404 not found error since /foobar doesn't exist in your /root fodder.
To solve this ,you need a RewriteRule to internally rewrite requests from /root to /subfolder .The following rule will Internally map http://example.com/foobar to http://example.com/subfolder/foobar .The redirection from root to the subfolder is invisible.
Add the following right bellow the subfolder to /root redirection rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /subfolder/$1 [L]
EDIT :
My apologies. I misread your question. If you want to redirect your /subfolder requests to just the root folder ,try this:
RewriteEngine on
RewriteCond %{THE_REQUEST} /subfolder/(.*)\sHTTP [NC]
RewriteRule ^.+$ / [NE,L,R]
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.
I am having problems writing some redirect rules. I want all the pages inside the "/es" folder to be redirected to my homepage but excluding the pages in /es/empresa that need to redirect to mydomain.com/nosotros/. I have written this code but it is not working:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/es/empresa/($|/)
RewriteRule / http://www.example.com/ [R=301,L]
You can use these 2 redirect rules:
RewriteEngine On
RewriteRule ^es/empresa(/.*)?$ /nosotros$1 [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]
I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
I'm looking to redirect my domain example.com and www.example.com to http://example2.com, but I want to keep all visits to my posts at http://example.com/sdfhs to continue through to their destinations.
Essentially, I ONLY want to redirect the root and www domain and leave the rest untouched.
How can I do this with .htaccess?
Don't know if you still have this issue, but I have had it on one of my domains (Yourls installation). Try:
RewriteEngine On
RewriteRule ^$ http://www.example.com/ [R=301,L]
It works on my installation, but check it out on your site.
Why not
RewriteEngine On
RewriteCond ${HTTP_HOST} example\.com|www\.example\.com
RewriteRule ^/?$ http://example2.com [L]
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\.)?example\.com$ [NC]
RewriteRule ^$ http://example2.com [L,R=301]
You could simply match any URL with at least 1 character (.+) and mark that as the last rule for the rewrite engine to look at [L].
Then afterwards redirect anything else (.*) to the new domain (the only other thing that wouldn't be matched up to that point would be the root).
RewriteEngine On
RewriteRule ^(.+)$ - [L]
RewriteRule ^(.*)$ http://example2.com/$1 [R=301,L]