Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janzikmund/a8d45210f2ef7c5ffd6d668e7f0bf48e to your computer and use it in GitHub Desktop.
Save janzikmund/a8d45210f2ef7c5ffd6d668e7f0bf48e to your computer and use it in GitHub Desktop.
Redirect indexed content of old website to new URLs, get list of indexed URLs

Redirect Old Indexed Website Content to new URLs

1. Grab all indexed URLs

  1. Install ginfinity chrome extension to allow infinite scrolling through Google results
  2. Search for all indexed urls using query like site:example.com
  3. Scroll through all the results (hope there is not too much :)
  4. Once all results are shown, open Dev Tools JS console and run this command (document.querySelectorAll('.g .r > a')).forEach(function(el) { console.log(el.href) });

2. Setup redirects in .htaccess

Single URL

#with or without trailing slash
RewriteRule ^contact/?$ "/contact.html" [R=301,L]

Using location hash

#hashes - use NE flag to no escape
RewriteRule ^back-pain/?$ "/index.html#scroll-services" [R=301,NE,L]

Remove query string if present

#either add ? to the end of target url, or for Apache >= 2.4 use QSD flag (query string discard)
RewriteRule ^blog/?$ "/index.html?" [R=301,L]

Based on query string value

RewriteCond %{QUERY_STRING} blogcategory=Compliance
RewriteRule ^(.*)$ /category/compliance/? [L,R=301]

Multiple URLs with single target, regexps

#regexp to cover more variants
RewriteRule ^product/(dumbells|blue-shoes|yellow-shoes)/?$ "/index.html?" [R=301,L]

Multipe URLs with multiple targets

#redirect any file with the .html extension to use the same filename but use the .php extension instead.
RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php

All URLs to a splash page, except existing assets

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ /? [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment