I am trying to redirect my entire site to a subfolder present in the root directory using the following Redirect command.
Redirect 301 / http://example.com/folder/
However, when I open the website, it gets redirected to something like
http://www.example.com/folder/folder/folder/folder/folder/folder/folder...
Am I doing something wrong here?
Yes, you're redirecting anything starting with / so of course that includes /folder/ and it just keeps redirecting. You can't redirect your whole site to a part of itself without excluding that part.
Use this instead:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [NE,R=301,L]
It will redirect anything that is not in /folder/.
To only redirect the homepage, use this instead:
RewriteEngine on
RewriteRule ^$ /folder/ [R=301,L]
Related
I have some files placed under a particular folder in my old domain:
http://www.olddmain.com/subfolder/example1.html
I want to redirect all files under this folder to a new domain.
Example:
http://www.newdomain.com/subfolder/example1.html
How do I do this without losing Ranking of the pages?
Maybe you need this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
from "Redirecting to a different domain" in Redirecting a Web Folder Directory to another Directory in htaccess
In the olddomain.com/subfolder/.htaccess file you just need a single mod_alias Redirect directive to redirect all files to newdomain.com/subfolder/<file>:
Redirect 302 /subfolder http://www.newdomain.com/subfolder
Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK. (301s are cached hard by the browser, so can make testing problematic.)
However, if olddomain.com and newdomain.com point to the same place then you will need to use mod_rewrite and include a condition that specifically checks for the host being requested, otherwise you'll get a redirect loop. For example, in the same olddomain.com/subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=302,L]
This redirects any request for olddomain.com/subfolder/<anything> to http://www.newdomain.com/subfolder/<anything>.
Again, change 302 to 301 when you are sure it's working OK.
I have a problem were I need to redirect www.mydomain.com to an external url www.otherdomain.com but I need all the subpages and urls to stay as they are (not get redirected).
Example www.mydomain.com/testpage should not get redirected. Is this possible?
And if so can anyone guide me what I need to write in my htaccess file for it to work.
The following .htaccess will do the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.\mydomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.otherdomain.com/ [L,R=302]
It will redirect only the base domain (root), all subfolders and other URLs will stay.
Reference:
https://serverfault.com/questions/58762/how-to-redirect-root-and-only-root-via-htaccess
I've been looking for a precise answer to this question for awhile but couldn't find it.
I've launched a one page website in place of an old website with many pages. Now, I want to redirect everything to www.domain.com. The page uses index.html as the homepage, but I don't want to redirect to that, I just want to redirect to the www.domain.com root.
I tried using:
RewriteRule ^.+$ / [R=302,NC,L]
But that just broke my stylesheet and didn't redirect anything. Other solutions I've seen have redirected to the index.html but I want to redirect to the / root domain.
Also, I want to be sure to redirect all non-www pages to www pages too. Can someone please help me out?
Much appreciated
You have most likely something like this in your httpd.conf:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
If you request a folder (and http://example.com/ is a request to a folder), it will try index.php first, then index.php3, etc, etc, and the first one that exists it will shown. You'll have to delete that from your httpd.conf if you want every request to end up as http://example.com. It'll show a directory view of your www-root folder unless that has been disabled.
Try:
# Any direct request for html/php pages
RewriteCond %{THE_REQUEST} /[^\?\ ]+\.(html?|php.) [NC]
RewriteRule ^ / [L,R=301]
That won't affect images, or style sheets, etc. Note that this matches against the %{THE_REQUEST} variable because internally the %{REQUEST_URI} gets converted to /index.html so you can't match against that.
If you want non-existent requests (which would normally result in a 404) to be redirected as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [L,R=301]
To force a "www" use:
RewiteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I have several domain aliases pointing to the same website like:
domain1.de/en/
domain1.de/de/
domain2.ch/de/
domain2.ch/en/
When someone opens a specific language-based sub-directory on a URL, I would like to redirect them to the start page, e.g. for domain1.de users shouldn't be able to access the sub-directory /en/ but be redirected to the start page. For the domain2.ch users should only have access to the directory /en/ and not /de/. How can I set this up with Htaccess?
This might be a helpful link to you;
htaccess redirect
for a 301 redirect entire directory it generated the following code:
//301 Redirect Entire Directory
RedirectMatch 301 domain2.ch/de/(.*) domain2.ch/en//$1
RedirectMatch 301 domain1.de/de/(.*) domain1.de/en//$1
Give this a try:
RewriteCond %{HTTP_HOST} ^de\.domain\.de$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)/en/(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/targetpage.php [R=301,L]
I'm trying to create a .htaccess that would for a subdomain "test" redirects all requests this way:
http://test.mydomain.com/something/something2 (1)
to
http://test.mydomain.com/www/something/something2 (2)
so that in browser's address bar is still the address (1)?
I have Apache 2.0.
I'm trying to write it for two hours and I can't still find the right way.
Thanks!
You'll want something like this:
RewriteEngine On
# Only redirect if we're on the subdomain and haven't redirected
# (internally) to /www/ yet
RewriteCond %{HTTP_HOST} ^test
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^.*$ /www/$0 [L]
# Let's also prevent them from being able to go to our /www path manually
RewriteCond %{THE_REQUEST} ^(POST|GET)\s/www
RewriteRule ^www/?(.*)$ http://test.mydomain.com/$1 [R,L]