Htaccess redirect without user knowing

Asked
Active3 hr before
Viewed126 times

9 Answers

htaccessredirectwithout
90%

I know you can redirect the client from a domain without www to the domain with www. But I found only solutions with entering the domain-name, like:,Connect and share knowledge within a single location that is structured and easy to search., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,The problem is, I want to redirect the user from domain example.com to domain www.example.com without knowing that it´s "example.com". So it could also be helloworld.com but should also redirect to www.helloworld.com.

You can do it this way

RewriteEngine on

RewriteCond % {
   HTTP_HOST
}! ^ www\.[NC]
RewriteCond % {
   HTTPS
}
on
RewriteRule ^ (.*) $ https: //www.%{HTTP_HOST}/$1 [R=301,L]

   RewriteCond % {
      HTTP_HOST
   }! ^ www\.[NC]
RewriteCond % {
   HTTPS
}
off
RewriteRule ^ (.*) $ http: //www.%{HTTP_HOST}/$1 [R=301,L]
88%

There are two reasons to avoid the use of .htaccess files. Let's take a closer look at them.,[ A free guide from Red Hat: 5 steps to automate your business. ] ,Force your website to HTTPS instead of HTTP

You should add the following configuration directive in the server's virtual host file to allow the .htaccess file in the DocumentRoot directory. If the following lines are not added, the .htaccess file will not work:

</VirtualHost>
<Directory /var/www/test.com/public_html>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
</Directory>
load more v
72%

Second, highlighted in blue, is the HTTP status code you want to use. This example uses 301 to indicate a permanent redirect. You can change this to whichever status code is most appropriate. If you do not include one, the system will automatically assume the redirect is temporary and use a 302 status code.,Maintaining a site is complicated work that requires attention to detail, and it’s easy to make mistakes. Make sure your site stays healthy by auditing your site regularly. ,.htaccess uses the same syntax as the root configuration, meaning any directives you would ordinarily add to the server configuration files can be used here.,For example, you’ll need to use this method if you want all URLs used to access your website to change automatically to HTTPS.

</VirtualHost>
<Directory  /var/www/test.com/public_html>Options Indexes FollowSymLinksAllowOverride AllRequire all granted</Directory>
load more v
65%

You can use the .htaccess file to force your site to only use https:// or only use www. as the subdomain. This can help ensure that your site does not have duplicate pages. Every search engine optimization (SEO) specialist knows that duplicate content is bad.,Let's use this on my site to see what happens.,For this one, we use the capture group (.*) which captures zero or more characters. This ensures we redirect any request to the website root that does not have www and https.,As you can see there is only one 301 redirect! This is better for your site than the redirect chain that many people use.

Here is a common way of fixing the problem with the .htaccess file.

# # Turn on rewrite engine
RewriteEngine on

# # Force WWW
RewriteCond % {
   HTTP_HOST
} ^ example\.com[NC]
RewriteRule ^ (.*) $ http: //www.example.com/$1 [L,R=301,NC]

   # # Force HTTPS
RewriteCond % {
   HTTPS
}!on
RewriteRule(.*) https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

   # # Remove trailing slash
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ (.*) / $ http: //www.example.com/$1 [L,R=301]

I will start by creating a .htaccess file in the site's root directory with the following code.

# # Turn on rewrite engine
RewriteEngine on

# # Force WWW
RewriteCond % {
   HTTP_HOST
} ^ danielmorell\.com[NC]
RewriteRule ^ (.*) $ http: //www.danielmorell.com/$1 [L,R=301,NC]

   # # Force HTTPS
RewriteCond % {
   HTTPS
}!on
RewriteRule(.*) https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

   # # Remove trialing slash
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ (.*) / $ http: //www.danielmorell.com/$1 [L,R=301]

Step 1: We will check to turn on the Rewrite Engine.

RewriteEngine On

We also want to determine if the URL ends in a trailing slash.

RewriteCond % {
   REQUEST_URI
}
/(.+)/$
RewriteCond % {
   REQUEST_FILENAME
}!-d

If both conditions are true we implement the following rewrite rule.

RewriteRule ^ https: //www.example.com/%1 [R=301,L]

Step 3: We need to enforce a trailing slash policy on directories.

RewriteCond % {
   REQUEST_URI
}!(. + ) / $
RewriteCond % {
   REQUEST_FILENAME
} - d

The second line makes sure we are working with a directory. This is important. If we left this part off we would have an endless redirect loop.

RewriteRule ^ (. + ) $ https: //www.example.com/$1/ [R=301,L]

Step 4: We need to check to see if www. is included in the requested URL and https is the protocol or scheme used.

RewriteCond % {
   HTTP_HOST
}! ^ www\.(.*) $[OR, NC]
RewriteCond % {
   https
}
off

The second line checks the protocol to see if it is https.

RewriteRule ^ (.*) $ https: //www.danielmorell.com/$1 [R=301,L]

Putting it all together. It should look like this.

# # # # Force HTTPS: //WWW and remove trailing / from files ####
   # # Turn on rewrite engine
RewriteEngine on

# Remove trailing slash from non - filepath urls
RewriteCond % {
   REQUEST_URI
}
/(.+)/$
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ https: //www.example.com/%1 [R=301,L]

   # Include trailing slash on directory
RewriteCond % {
   REQUEST_URI
}!(. + ) / $
RewriteCond % {
   REQUEST_FILENAME
} - d
RewriteRule ^ (. + ) $ https: //www.example.com/$1/ [R=301,L]

   # Force HTTPS and WWW
RewriteCond % {
   HTTP_HOST
}! ^ www\.(.*) $[OR, NC]
RewriteCond % {
   https
}
off
RewriteRule ^ (.*) $ https: //www.example.com/$1 [R=301,L]

Most sites are at least using the two-redirect method. It is slightly better than the three-redirect method. It looks something like this.

# # # # Force HTTPS: //WWW and remove trailing / from files ####

   RewriteEngine on

# # Force https and www
RewriteCond % {
   HTTPS
}
off[OR]
RewriteCond % {
   HTTP_HOST
}! ^ www\.example\.com$[NC]
RewriteRule ^ (.*) $ https: //www.example.com/$1 [L,R=301]

   # # Remove trailing slash
if not directory
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteCond % {
   REQUEST_URI
}(. + ) / $
RewriteRule ^ (.*) / $ https: //www.example.com/$1 [R=301,L]
load more v
75%

Temporary redirect — A temporary redirect, or 302 redirect, should be used if you want to temporarily point a user to another location.,Permanent redirect — A permanent redirect, or 301 redirect, should be used anytime you permanently move a page, directory or website.,Redirect #3 — Shows that any subdirectories within the old directory are automatically redirected to the same location within the new directory.,Redirect #6 — Subdirectories are redirected to the same location on the new domain.

This is the basic syntax for redirects written using the mod_alias redirect directive in Apache:

Redirect[status] URL - path URL
load more v
40%

Are you actually looking for a redirect, or to have that other site's content appear under your original URL (more proxying)? I'll assume that the other server is your content and either internal/backend or if public you're aware of the negative SEO implications. If someothersite.com is not your site and content, you probably shouldn't be doing this.,If your trying to show the content of the other site without the user seeing the other url, you'll need both mod_rewrite and mod_proxy, Reconnecting with a previous professor then asking right away for a reference letter , Hm, good point. I've probably read the question wrong: I see that he wants to redirect EVERY URL from somesite.com to someothersite.com; however, this could use some clarification. – Christian Paredes Jul 14 '10 at 4:25

If you're trying to redirect just a subdirectory of the server, you can use mod_rewrite like this:

RewriteCond % {
   REQUEST_URI
}
/some/url [NC]
RewriteRule ^ (.*) $ other.example.com / $1[R]

If your trying to show the content of the other site without the user seeing the other url, you'll need both mod_rewrite and mod_proxy

RewriteCond % {
   REQUEST_URI
}
/some/url [NC]
RewriteRule ^ (.*) $ http: //other.example.com/$1 [P]
load more v
22%

You can use a 301 redirect, but replacing an existing domain name with another domain name to mask the origination of the link is not something we typically allow in our hosting service. If you are not hosting with our service, then you may need to consult with your host’s service to understand how this may be done., InMotionFan says: July 18, 2018 at 5:50 pmYou can use a 301 redirect, but replacing an existing domain name with another domain name to mask the origination of the link is not something we typically allow in our hosting service. If you are not hosting with our service, then you may need to consult with your host’s service to understand how this may be done.Reply ,Thanks for the comment. If your domains are already set to InMotion’s name servers, then you can use the cPanel Domain Re-directs.,There is nothing after the URL. Not in that example, or in any example. The whole thing is the URL. I assume he means to say the domain name or the TLD.

To do this, you would modify your .htaccess file for the domain that your users will go to, and insert these lines of code:

RewriteCond % {
   HTTP_HOST
} ^ DomainA.comRewriteRule ^ (.*) https: //DomainB.com/$1 [P]

There is another way you can do your redirect to show a specific URL, but keep the domain the same as well. If you want visitors to go to DomainA.com with a specific page in mind when doing so, you may use this code:

RewriteCond % {
   HTTP_HOST
} ^ DomainA.com
RewriteRule ^ (.*) https: //DomainB.com/PathToPageHere [P]

Occasionally, there will be a request to re-direct an IP address to a specific URL. The following code shows how this can be done in the .htaccess file.

# Redirect all IP address(replace the # # with the IP address numerals) to same https: //domain_name.com
   RewriteCond % {
      HTTP_HOST
   } ^ # #.# #.# #.# #
RewriteRule(.*) https: //domain_name.com/$1 [R=301,L]
load more v
60%

Hi, please can you help with this redirection:,Inside the htaccess file of the subdirectory I have placed:,Thank you very much for this incredible helpful gist :) everything I need to know about .htaccess files in one place 👍,How can i redirect this url

  • redirect all traffic from old domain to new domain, in same hosting
  • new domain is add-on domain
  • wordpress is installed in folder (not in root)
  • placed on top of the .htaccess in yourblogfolder/ (the wordpress installation folder)
RewriteCond % {
   HTTP_HOST
} ^ old\.com$[OR]
RewriteCond % {
   HTTP_HOST
} ^ www\.old\.com$
RewriteRule ^ (.*) $ "http\:\/\/new\.com\/yourblogfolder\/$1" [R = 301, L, QSA]
load more v
48%

I use the 301 redirect to remove “www” from the domain name. I noticed that css-tricks also have your “www” removed. I wonder if that have an effect on page popularity (link history) for older web pages with “www”.,I’m not understanding your question. The format for 301 redirects are:,Are you sure the whole URL is required if I’m redirecting within the same domain?,or the short version: Redirect 301 / http://myDomain.com/

Redirect a single page

Redirect 301 / oldpage.html http: //www.yoursite.com/newpage.html
   Redirect 301 / oldpage2.html http: //www.yoursite.com/folder/

This way does it with links intact. That is www.oldsite.com/some/crazy/link.html will become www.newsite.com/some/crazy/link.html. This is extremely helpful when you are just “moving” a site to a new domain. Place this on the OLD site:

Redirect 301 / http: //newsite.com/
load more v

Other "htaccess-redirect" queries related to "Htaccess redirect without user knowing"