isaacadams

Redirects

Oct 7th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #301 Redirects for .htaccess
  2.  
  3. #Redirect a single page:
  4. Redirect 301 /pagename.php http://www.domain.com/pagename.html
  5.  
  6. #Redirect an entire site:
  7. Redirect 301 / http://www.domain.com/
  8.  
  9. #Redirect an entire site to a sub folder
  10. Redirect 301 / http://www.domain.com/subfolder/
  11.  
  12. #Redirect a sub folder to another site
  13. Redirect 301 /subfolder http://www.domain.com/
  14.  
  15. #This will redirect any file with the .html extension to use the same filename but use the .php extension instead.
  16. RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
  17.  
  18. ##
  19. #You can also perform 301 redirects using rewriting via .htaccess.
  20. ##
  21.  
  22. #Redirect from old domain to new domain
  23. RewriteEngine on
  24. RewriteBase /
  25. RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
  26.  
  27. #Redirect to www location
  28. RewriteEngine on
  29. RewriteBase /
  30. rewritecond %{http_host} ^domain.com [nc]
  31. rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
  32.  
  33. #Redirect to www location with subdirectory
  34. RewriteEngine on
  35. RewriteBase /
  36. RewriteCond %{HTTP_HOST} domain.com [NC]
  37. RewriteRule ^(.*)$ http://www.domain.com/directory/index.html [R=301,NC]
  38.  
  39. #Redirect from old domain to new domain with full path and query string:
  40. Options +FollowSymLinks
  41. RewriteEngine On
  42. RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]
  43.  
  44. #Redirect from old domain with subdirectory to new domain w/o subdirectory including full path and query string:
  45. Options +FollowSymLinks
  46. RewriteEngine On
  47. RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$
  48. RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]
  49.  
  50. Rewrite and redirect URLs with query parameters (files placed in root directory)
  51.  
  52. Original URL:
  53.  
  54. http://www.example.com/index.php?id=1
  55. Desired destination URL:
  56.  
  57. http://www.example.com/path-to-new-location/
  58. .htaccess syntax:
  59.  
  60. RewriteEngine on
  61. RewriteCond %{QUERY_STRING} id=1
  62. RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
  63. Redirect URLs with query parameters (files placed in subdirectory)
  64.  
  65. Original URL:
  66.  
  67. http://www.example.com/sub-dir/index.php?id=1
  68. Desired destination URL:
  69.  
  70. http://www.example.com/path-to-new-location/
  71. .htaccess syntax:
  72.  
  73. RewriteEngine on
  74. RewriteCond %{QUERY_STRING} id=1
  75. RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]
  76. Redirect one clean URL to a new clean URL
  77.  
  78. Original URL:
  79.  
  80. http://www.example.com/old-page/
  81. Desired destination URL:
  82. http://www.example.com/new-page/
  83. .htaccess syntax:
  84.  
  85. RewriteEngine On
  86. RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]
  87. Rewrite and redirect URLs with query parameter to directory based structure, retaining query string in URL root level
  88.  
  89. Original URL:
  90.  
  91. http://www.example.com/index.php?id=100
  92. Desired destination URL:
  93.  
  94. http://www.example.com/100/
  95. .htaccess syntax:
  96.  
  97. RewriteEngine On
  98. RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
  99. Rewrite URLs with query parameter to directory based structure, retaining query string parameter in URL subdirectory
  100.  
  101. Original URL:
  102. http://www.example.com/index.php?category=fish
  103. Desired destination URL:
  104. http://www.example.com/category/fish/
  105. .htaccess syntax:
  106.  
  107. RewriteEngine On
  108. RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
  109. Domain change – redirect all incoming request from old to new domain (retain path)
  110.  
  111. RewriteEngine on
  112. RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
  113. RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
  114. If you do not want to pass the path in the request to the new domain, change the last row to:
  115.  
  116. RewriteRule ^(.*)$ http://www.example-new.com/ [R=301,L]
  117.  
  118. #From blog.oldsite.com -> www.somewhere.com/blog/
  119. retains path and query, and eliminates xtra blog path if domain is blog.oldsite.com/blog/
  120. Options +FollowSymLinks
  121. RewriteEngine On
  122. RewriteCond %{REQUEST_URI}/ blog
  123. RewriteRule ^(.*) http://www.somewhere.com/%{REQUEST_URI} [R=302,NC]
  124. RewriteRule ^(.*) http://www.somewhere.com/blog/%{REQUEST_URI} [R=302,NC]
Add Comment
Please, Sign In to add comment