Welcome to WebmasterWorld Guest from 54.152.38.154

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Is my redirect wrong?

     
11:25 pm on Aug 28, 2016 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


I placed a link to my site on Instagram. When visitors click the link, they see a warning that says something like this:

You followed a link on Instagram that redirects to: http://example.com. Remember, only follow links from sources that you trust. We are showing you this message because our systems detected that it may not take you to where you want to go.


Why are people seeing this warning? It's not a generic notice telling them that they are leaving Instagram. It's an actual warning, saying that my site redirects them.

The link on Instagram looks like this: http://example.com

It's a link straight to my home page. There's no "www.".

In my .htaccess file, I have this:

RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]


Do you think this redirect code is causing the warning? All this code does is ensure that "www." isn't included in the domain name.
12:31 am on Aug 29, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


That redirect code looks OK to me and shouldn't cause the issue mentioned. As you suggest, it does not apply to URLs of the form you mention.

You followed a link on Instagram that redirects to: http://example.com.


...saying that my site redirects them.


That doesn't necessarily mean that "your site redirects", but whatever site is in the link, redirects. If the link URL and the site it redirects to is the same, then that doesn't really make sense IMO.
12:46 am on Aug 29, 2016 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4297
votes: 288


The rule you shared is not the standard format for a non-www canonicalization rewrite. The basic rule usually looks something like:
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

I'm no expert but I don't think your rule is doing what you want it to do.
4:53 pm on Aug 29, 2016 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


Thanks for the replies, guys.

If the link URL and the site it redirects to is the same, then that doesn't really make sense IMO.


Yep, they're exactly the same.

I'm no expert but I don't think your rule is doing what you want it to do.


Interesting. I don't know anything about canonical redirects.

So what's the best rule to use to ensure non-www?
5:23 pm on Aug 29, 2016 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4297
votes: 288


The rule is shown in my previous post. That's a basic rule to rewrite requests for www to non-www requests. It rewrites http://www.example.com as http://example.com and http://www.example.com/page5.html as http://example.com/page5.html

If you really want to know what happens to your link at Instagram when it tries to access the URL, use a tool to see the live headers during the request and then you can see whether it is requesting the URL you expect it to reach. I use the FF Extension: "Live HTTP Headers" but there are many free tools you can use to view headers.
4:57 pm on Aug 30, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


The basic rule usually looks something like:

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]



This should work for redirecting to a single domain. (However, I would remove the parentheses and ? suffix on the CondPattern - they would seem to be superflous (and confusing IMO)? ie. !^example\.com$)

I'm no expert but I don't think your rule is doing what you want it to do.


Interesting. I don't know anything about canonical redirects.


The original redirect in the OP would seem to be a form of multi-domain canonical redirect. It would handle the "www.example.com" to "example.com" canonical redirect just fine (plus the respective URL-path). However, it also handles any "www" host that ends in "example.com". So it would also redirect "www.fooexample.com" to "fooexample.com" and "www.barexample.com" to "barexample.com".

So, unless you have multiple domains (on the one account) then you should certainly go for a simpler canonical redirect, like the one that not2easy suggests above.
11:30 pm on Aug 30, 2016 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


Thank you, whitespace and not2easy!

I have used this code:

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


As you can see, I followed both of your instructions, as well as added "[NC]".

It seems to be working fine for now. I've even stopped getting the warning message! However, the warning message came and went. If it comes back, I shall try the brilliant suggestion by not2easy and check the HTTP headers. :-)

Last question:

I should have mentioned that my external redirects also include this line, which comes before the canonical redirect:

RewriteRule ^index\.html$ / [R=301,L]


Forgive my ignorance. What does that do?
1:26 am on Aug 31, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


...as well as added "[NC]".


Actually, since it is a negated regex, you should remove the NC flag. You need the rule to be processed when the HOST is not "example.com" (all lowercase). Whilst browsers always lowercase the host in the request anyway, you need to also catch malformed requests from non-browsers. This is the reason for the NC flag on your initial RewriteCond directive.

RewriteRule ^index\.html$ / [R=301,L]

Forgive my ignorance. What does that do?


It's another canonical redirect that redirects "example.com/index.html" to "example.com/" (the document root). These two URLs presumably return the same content (otherwise there is no need for this directive). mod_dir will internally rewrite requests for the directory (eg. "example.com/") to the specified DirectoryIndex (which I assume is "index.html").

However, you do have to be careful of such redirects as you can get a redirect loop.
2:06 am on Aug 31, 2016 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4297
votes: 288


The file that is shown when someone visits your home page at http://example.com is commonly "index.html" by default. That rule sends requests for index.html specifically as in http://example.com/index.html to the home page at http://example.com because you want to avoid duplicate content by offering the same content at two different URLs. For the same reason, you should never use a link to /index.html. Not all sites have an index.html file so I'm curious - is there an "index.html" file? If not, the rule is not needed.
11:25 am on Aug 31, 2016 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


Thank you guys both very much! :-D

Okay, this is what my entire .htaccess file now looks like:

# SETUP
IndexOptions +Charset=UTF-8
RewriteEngine On


# SECTION 1: BLOCK BAD REQUESTS
# None


# SECTION 2: EXTERNAL REDIRECTS
RewriteRule ^index\.html$ / [R=301,L]

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


# SECTION 3: INTERNAL REWRITES
RewriteRule ^category/([/_0-9a-zA-Z-]+)$ /category.php?id=$1 [L]
RewriteRule ^article/([/_0-9a-zA-Z-]+)$ /article.php?id=$1 [L]


Can you see any potential problems?

Yes, I do have an index.html file.

I'll be very grateful for your feedback. :-)
12:10 pm on Aug 31, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


Assuming requests for "example.com/index.html" are correctly redirected to "example.com/" (ie. no redirect loop) then the only thing I would perhaps consider changing is the substitution, to make it an absolute URL with the correct canonical domain. For example:


RewriteRule ^index\.html$ http://example.com/ [R=301,L]


Otherwise, if a user did request "www.example.com/index.html" then they would experience 2 redirects... the first one for the "index.html" canonicalisation and the second for the "www" canonicalisation. Or, these rules can be combined, but there is something to be said for keeping them separate. But this is a small thing / hopefully an edge case anyway.

Then there are those that say you should always use a full absolute URL for external redirects. It is certainly "safer" (less prone to error), however, for maintainability I do tend to favour the root-relative URL where possible.
2:10 pm on Aug 31, 2016 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


Thank you, whitespace!

I have changed the substitution to an absolute URL, as you suggested. Working well!

I also checked the HTTP headers as suggested by not2easy. My problem was that when someone clicked the link to my site on Instagram, they'd get a warning saying that my link might not take them to where they want to go. The link goes straight to my home page, so it was a mystery. Having checked the HTTP header requests, there don't seem to be any problems. There's no redirecting going on. Hopefully, the problem will go away.

I appreciate your and not2easy's help. Lots of "Like" votes for you guys.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members