Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have seen many suggestions on various redirects, but none were simple and many had no accepted answer.

I have a site I wish to completely remove from google and have only my homepage available

In that homepage I have image css and js files so they of course should not be redirected

My plan was to redirect all .html and all .php that are not the /index.html in root to the /index.html in root

Of course / should also be allowed.

So /js, /css, /img and /images should be left alone

Any other php or html page I thought I wanted to have 301 to /index.html

This worked but as pointed out in a comment, does not tell Google that the content that it indexed is no longer supposed to be there

Stackoverflow: how-to-redirect-all-pages-only-to-index-html-using-htaccess-file-and-not-redirect

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=301]

So my amended question is

How to tell google my content is gone and redirect all requests for content (bookmarked pages for example or external links) made to my site to /index.html

Update

ErrorDocument 410 /error-docs/error410.html

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/error-docs/
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* - [G]

almost works

But I want to return 410 on all files in 4 subfolders and whatever is under them

I have

/index.html
/images/
/js/
/css/
/unwantedfolder/with/stuff/imagesandhtml
/anotherunwantedfolder/with/stuff/imagesandhtml

I want to give 410 for now on all request to anywhere in the unwanted folders

If I add

RewriteRule ^unwantedfolder - [G]

like this

ErrorDocument 410 /error-docs/error410.html

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/error-docs/
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* - [G]

RewriteRule ^unwantedfolder - [G]

nothing happens to

http://www.myserver.com/unwantedfolder/bla/images/someimage.png

It shows without any redirection, likely due to the !\.(gif|jpe?g|png|css|js)$ earlier whereas

http://www.myserver.com/unwantedfolder/bla/somepage.html 

does get a error410 page

share|improve this question
    
Do these other pages still exist? –  w3d Apr 10 at 21:29
    
Yes they all exist –  mplungjan Apr 11 at 4:37
    
You seem to be focused on redirection, however, if you want to "completely remove from Google" (quickly) then you probably want to return a 410 (Gone), instead of redirecting. –  w3d Apr 11 at 9:06
    
@W3d That is a good idea. See my updated question. I wonder if I should then simply add .htaccess files in my 4 sub dirs that are to be "gone" and have them return 410 for all content from there? –  mplungjan Apr 11 at 11:29

3 Answers 3

up vote 1 down vote accepted

Following on from comments... since you are wanting to completely remove these pages from Google's index then simply redirecting (301) them (as requested in your original question) is not necessarily the correct thing to do. Redirection is saying that the page has moved. Yes, Google is likely to drop the original page from the index... eventually, but that could take some time. Trying to preserve PR by redirecting all pages to the homepage is unlikely to provide the SEO benefit you might hope for, and this is generally confusing for users.

I would suggest serving a custom 410 (Gone) for these pages, with a prominent link to the homepage (if you wish) and not actually send the user to the homepage directly - unless your homepage is your 410!?

Modifying your current .htaccess rules:

ErrorDocument 410 /error-docs/e410.html

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/error-docs/
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule . - [G]

The single hyphen (-) in the RewriteRule substitution passes the URL through unchanged. The G (GONE) flag returns a 410 status code and results in your custom 410 being served. An exception for the /error-docs/ folder is also required.

CHANGE: Note, I've changed the RewriteRule pattern from .* (meaning "anything") to simply . (single period) (meaning "something"). This is an alternative to specifying an additional RewriteCond directive for the root URL. So, the following is unnecessary:

RewriteCond %{REQUEST_URI} !=/

This should ensure that Google will remove these pages as-soon-as. You should also be able to see confirmation of this in terms of a crawl error report in GWT (yes, it is a crawl error, but it is intentional). This also provides a meaningful message to users and should encourage them to update/delete their bookmarks as required.

UPDATE: As mentioned in comments, the above rules still permit all the "gallery" images to be accessed (in a sub folder). In order to prevent the gallery images, we can add another RewriteRule following the directives above:

# (Above directives go here...)

RewriteRule ^gallery\d - [G]

This will block all URLs (including images) that start /gallery1, /gallery2, etc. (Note that the / prefix is intentionally omitted from the RewriteRule pattern.) However, the directives at the top will still allow all the other images, necessary to build your homepage.

Note that this second RewriteRule is entirely separate from the previous RewriteRule and RewriteCond directives above. RewriteCond directives only apply to the single RewriteRule that follows them. So, the RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$ does not apply to this second RewriteRule.

Summary

The following is the complete set of rules:

ErrorDocument 410 /error-docs/e410.html

RewriteEngine on

# Serve 410 to all files except:
# error documents, /index.html, / (root) and images
RewriteCond %{REQUEST_URI} !^/error-docs/
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule . - [G]

# Serve 410 to EVERYTHING within the /unwantedfolder
# >>> including images <<<
RewriteRule ^unwantedfolder - [G]

# Serve 410 to EVERYTHING within the /anotherunwantedfolder
RewriteRule ^anotherunwantedfolder - [G]
share|improve this answer
    
Thanks, I will try that. So my 410 page could redirect with script to my homepage instead making Google see the 410, the users see the 410 too for a little while with explanation and then get directed to the homepage... I assume this might not work in IE with "friendly error messages" turned on –  mplungjan Apr 11 at 14:28
    
Yes, I think you could do a client-side (200) redirect with JavaScript or meta refresh after a period of time if you want to. To avoid the "friendly error messages" in IE just ensure that your error document is above a certain size (512 bytes I think) - which you can do just by including a large HTML comment. –  w3d Apr 11 at 14:40
    
Ohhh, nice :).... I'll accept once I test this –  mplungjan Apr 11 at 14:40
    
Ok almost. Since the CONTENT but not the path is replaced by the 410 html, all css has to be absolute or the page will be really ugly –  mplungjan Apr 11 at 17:25
1  
Seems to work well. Thanks! I had to allow robots.txt and some webmaster google fies too –  mplungjan Apr 14 at 20:22

How about:

RewriteRule (html|php)$ http://www.example.com [R=301,L]

This matches all requests which end either with html or php strings.

share|improve this answer
    
Creates a redirect loop. This one worked stackoverflow.com/questions/17709500/… –  mplungjan Apr 11 at 4:43
    
@mplungjan: if you found a solution to your question, please create an answer (to explain what you did) and accept it. Thanks in advance. –  Zistoloen Apr 11 at 12:14
    
Sort of. Please see amended question –  mplungjan Apr 11 at 13:02

You can try these lines into a .htaccess file:

RewriteRule ^(html|php) http://www.example.com [R=301,L]
share|improve this answer
    
No. Not at all. –  mplungjan Apr 10 at 20:46
2  
This RewriteRule matches URIs that start with html or php, quite the opposite from what was requested. –  Tero Kilkanen Apr 11 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.