Forum Moderators: phranque

Message Too Old, No Replies

Smart HTTP HTTPS RewriteRule Redirects

Robust HTTPS or HTTP RewriteRules with Environment Variable

         

webalot

4:12 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



A while back, jdMorgan, one of the obviously high skilled moderators, came up with a easy, practical way to deal with HTTP and HTTPS rewrites. The answer was as follows:

# redirect urls with index.html to folder
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http%2://%{HTTP_HOST}/$1 [R=301,L]
#

This can be found at [webmasterworld.com...]

The problem I am having is try to create a search engine friendly 301 redirect from a ".co.uk" and ".org" to a ".com" whilst making sure it goes forces "www." at the beginning.

The result I have come up with works but I for some reason can not get my head around implementing it to work with HTTP and HTTPS correctly or in a simpler way. The code current code is as follows:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=askapache:https]
RewriteCond %{HTTPS}!=on
RewriteRule ^(.+)$ - [env=askapache:http]

RewriteCond %{HTTP_HOST} ^thedomain.com [OR]
RewriteCond %{HTTP_HOST} ^thedomain.co.uk [OR]
RewriteCond %{HTTP_HOST} ^thedomain.org [OR]
RewriteCond %{HTTP_HOST} ^www.thedomain.co.uk [OR]
RewriteCond %{HTTP_HOST} ^www.thedomain.org [NC]
RewriteRule ^(.*)$ %{ENV:askapache}://www.thedomain.com%{REQUEST_URI} [L,R=301]

I would appreciate any help with this. Thanks for your time.

jdMorgan

5:11 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code's over-complicated... Try:

# Externally redirect to canonical domain using HTTP or HTTPS
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule (.*) http%2://www.example.com/$1 [R=301,L]

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

I'd suggest that you open the Apache mod_rewrite documentation, and using the RewriteCond and RewriteRule directive descriptions, go through this code character-by-character until you understand exactly what it is doing. Putting code you don't understand on a server --and especially one that evidently needs SSL-- is not a good idea.

This code is intended for use in you DocumentRoot .htaccess file. FOr use in httpd.conf, conf.d, or some other server config file, change the RewriteRule pattern to "^/(.*)$"

Jim

webalot

5:38 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



Thank you for the quick reply.

Just looked through it now with the mod_rewrite docs. Thank you. I have only been doing mod rewrites for 1 day and I've been trying to get my head round it all. I have gone through it but I am wondering, will this pass on the good a search engine has collected on it's indexed pages of the site to the page requested or to the index/home page only?

For example, if I type:

"http://www.thedomain.co.uk/thepageneeded.php"

and this is currently indexed high on Google, will it pass on the good it has on Google Page Rank and others search engines to

"http://www.thedomain.com/thepageneeded.php"

or to

"http://www.thedomain.com/"

as the intention is to replace the .co.uk with .com on the search engine results without losing it's position to much.

Sorry for being so basic but I'm trying to learn. Thanks for your help.

jdMorgan

6:43 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is an SEO question, but I'll try to summarize what you'd find in the Google forum: A 301 redirect tells the search engine to replace the old URL with the new URL, and transfer any rank held by the old URL over to the new one.

Note that I said "URL." While there is some "awareness" of other concepts like "sites" and "domains" at some point in Google's processing associated with filters and real penalties (which are very rare), those are minor cases, and it is safe to say that Google works using *only* URLs. The URL is the only "unit" that it works on.

Redirecting using a 301-Moved Permanently status --as in this rule-- tells the search engine to transfer any PageRank/Link-popularity assigned to the old URL over to the new URL. However, this can take time. How long it takes depends on how often the search engine spiders your URLs, how often it updates its ranking data and index and --in the case of Yahoo and MSN-- whether the search engine's 301-handling is broken this month or not.

If your site gets frequently-spidered and the engine's search-ranking data is updated fast (due to lucky coincidence), then this might take a week or two. If your site is infrequently-spidered or if you miss an update, then it may take several months.

However, what is going to happen worst-case is that the non-canonical domain URLs will lose their ranking soon, but that ranking won't get passed to the new URLs until later. So you may first see the non-canonical-domain pages drop, and then you'll later see the canonical-domain-pages get a boost. Again, the timing may or may not work out that way, and any question about how long it will take would require data that only a Google search engineer would have access to. All we can do is guess, "anywhere from three days to nine months -- Highly-ranked, well-linked sites sooner, obscure, poorly-linked sites later."

I cannot answer the domain-related specifics in the question you posed because they don't agree with the example domain in the code I posted. The code I posted will transfer any ranking power held by any and all non-canonical variants of "www.example.com" over to "www.example.com". The code specificaly says, "If the requested domain is NOT *exactly* "www.example.com" or blank, then redirect to the corresponding URL-path at "www.example.com".

You have to allow for a blank hostname in order to prevent an infinite loop if/when you get an HTTP/1.0 request (which will not contain a Host header, leaving that value blank). While this is only required if your server has a unique IP address, I always include it so as not to leave a "time bomb" in the code in case you later move to a server with a unique IP address.

Be extremely-careful when changing the RewriteCond pattern and the substitution URL over to your actual domain; The two must match exactly, with the exception of the required regular-expressions character escaping and pattern-anchoring tokens shown in my example. Once installed, test thoroughly using a server headers checker such as the "Live HTTP Headers" add-on for Firefox/Mozilla browsers. Be sure that you seen one and only one 301-Redirect response from the non-canonical URL to the canonical URL, using any and all domain and URL variations you can come up with.

Using mod_rewrite for one day... Well, you certainly picked a 'big' application for your first try! ;)

Jim

webalot

11:11 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



Sorry for the SEO question in the Apache Web Server section. Your solutions and detailed information on the process has helped me learn much. I hope I can return the help to someone else once I learn and know more.

Thank you for all your help.