Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created February 2, 2012 04:30
Show Gist options
  • Save ScottPhillips/1721489 to your computer and use it in GitHub Desktop.
Save ScottPhillips/1721489 to your computer and use it in GitHub Desktop.
Common .htaccess Redirects
#301 Redirects for .htaccess
#Redirect a single page:
Redirect 301 /pagename.php http://www.domain.com/pagename.html
#Redirect an entire site:
Redirect 301 / http://www.domain.com/
#Redirect an entire site to a sub folder
Redirect 301 / http://www.domain.com/subfolder/
#Redirect a sub folder to another site
Redirect 301 /subfolder http://www.domain.com/
#This will 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
##
#You can also perform 301 redirects using rewriting via .htaccess.
##
#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
#Redirect to www location
RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
#Redirect to www location with subdirectory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/directory/index.html [R=301,NC]
#Redirect from old domain to new domain with full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]
#Redirect from old domain with subdirectory to new domain w/o subdirectory including full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$
RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]
Rewrite and redirect URLs with query parameters (files placed in root directory)
Original URL:
http://www.example.com/index.php?id=1
Desired destination URL:
http://www.example.com/path-to-new-location/
.htaccess syntax:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
Redirect URLs with query parameters (files placed in subdirectory)
Original URL:
http://www.example.com/sub-dir/index.php?id=1
Desired destination URL:
http://www.example.com/path-to-new-location/
.htaccess syntax:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]
Redirect one clean URL to a new clean URL
Original URL:
http://www.example.com/old-page/
Desired destination URL:
http://www.example.com/new-page/
.htaccess syntax:
RewriteEngine On
RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]
Rewrite and redirect URLs with query parameter to directory based structure, retaining query string in URL root level
Original URL:
http://www.example.com/index.php?id=100
Desired destination URL:
http://www.example.com/100/
.htaccess syntax:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
Rewrite URLs with query parameter to directory based structure, retaining query string parameter in URL subdirectory
Original URL:
http://www.example.com/index.php?category=fish
Desired destination URL:
http://www.example.com/category/fish/
.htaccess syntax:
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
Domain change – redirect all incoming request from old to new domain (retain path)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
If you do not want to pass the path in the request to the new domain, change the last row to:
RewriteRule ^(.*)$ http://www.example-new.com/ [R=301,L]
#From blog.oldsite.com -> www.somewhere.com/blog/
retains path and query, and eliminates xtra blog path if domain is blog.oldsite.com/blog/
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*) http://www.somewhere.com/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) http://www.somewhere.com/blog/%{REQUEST_URI} [R=302,NC]
@Fiona854
Copy link

Fiona854 commented Mar 4, 2025

This is a comprehensive guide on implementing 301 redirects using .htaccess extremely useful for SEO and maintaining site structure during domain changes or updates! If you’re looking for reliable plumbing services to keep your home’s systems running smoothly, check out my plumbing site at Plumbers Near Me . We’re here to help with all your plumbing needs, just like this guide helps with redirects!

@Zaryaab74
Copy link

Check Your VAT From Calcvat.

@magicsoft123
Copy link

https://ytmp3mate.com youtube video downloader

Support Full HD, 1080p, 2k, 4k

@Bentennysons
Copy link

Looking for an easy way to enhance your brand’s visibility? custom hoodies are a stylish solution. Embroidered and printed designs create a professional look. Soft, high-quality materials provide all day comfort. Employees and customers love wearing branded apparel. Get high-quality customised hoodies to showcase your business.

@henryhickson
Copy link

يُعتبر GitHub منصة رائدة لمشاركة الأكواد والمشاريع البرمجية بين المطورين، تمامًا كما تُعد لعبة طاولة بلوت وسيلة ممتعة للتفاعل والتعاون بين الأصدقاء والعائلة.

@San-Andreas-Apk
Copy link

GTA San Andreas APK (gtasanapks.com) remains one of the most popular open-world action games for Android, offering a nostalgic yet thrilling experience. The latest APK version brings improved graphics, smoother controls, and enhanced compatibility with modern devices. Rockstar Games has optimized gameplay performance, fixing previous bugs and ensuring better stability. Players can explore the vast map of San Andreas, complete missions, and enjoy classic cheat codes for extra fun. With continued fan support, GTA San Andreas APK remains a timeless masterpiece in mobile gaming.

@Johnesjensonss
Copy link

Visit https://beingfiler.com/cm-punjab-asaan-karobar-finance-asaan-karobar-card/ to get complete details about CM Punjab Asaan Karobar Finance and Asaan Karobar Card. Learn about eligibility, benefits, and the application process in one place

@Geo3453
Copy link

Geo3453 commented Mar 8, 2025

Smart SIM Card registration in the Philippines can be sometimes tricky for applicants. To make this process smooth and easy an applicant should See information here

@Geo3453
Copy link

Geo3453 commented Mar 8, 2025

Instander can make your Instagram experience worth even more than you expected. It can add more control be adding MOD features in it. Check now which features can be your most favorite one

@sastaemail10
Copy link

Great resource! Managing clinical trial sites efficiently is crucial for successful research. At Site Management Organization , we specialize in SMO services

@sohaibpro12
Copy link

Informative, If you have any plans to play Hero Games Then you should download and play Rope Hero MOD APK

@Sparksages09
Copy link

Looking for the safest way to play Infinity Brawl? Download from infinitybrawl.com and unlock the best features, including unlimited gems, powerful heroes, and exciting game modes. Experience a seamless and action-packed gaming experience today.

@calculadoraigv12
Copy link

Fantastic post! I truly enjoyed your perspective—it’s refreshing to see how traditional elements are being reinterpreted in modern design. The evolution of water pipes, in particular, is fascinating. I recently explored some innovative designs from Lotus Glass USA, where they blend artistry with functionality in remarkable ways. It’s exciting to see how craftsmanship and cutting-edge techniques are shaping the aesthetics and performance of these pieces. Your insights have sparked my curiosity to delve deeper into this evolution, and I’d love to see more discussions on this in future posts. Keep up the great work!

@8ballpool1048
Copy link

8ballpool1048 commented Mar 10, 2025

The 8 Ball Pool Mod APK makes the game so much more enjoyable with unlimited coins and cash. It’s a game-changer for leveling up quickly.

@ZellRaihan
Copy link

Check out the latest Irish Lotto results. The winning numbers of the Irish Lottery draws are live within minutes every Wednesday and Saturday (T&Cs Apply)

@UnknownUser59
Copy link

Informative...

@khizerhayat09
Copy link

Great post! The information is well-explained and very useful. It’s always helpful to see practical examples like this. Do you think this method works best for beginners, or is it more suited for advanced users? Also, if anyone is interested in growing their Instagram with free followers and likes, Instaup mod APK is a great tool to check out. Looking forward to more insights like this!

@alex23232-tech
Copy link

Looking to streamline your HR operations? Partner with the Best HR Outsourcing Provider in Pakistan for end-to-end HR solutions, including payroll, compliance, and workforce management, so you can focus on business growth.

@Steve5463
Copy link

Jeem.pk has earned a reputation for excellent customer service, addressing queries and issues promptly. The support team is dedicated to ensuring a smooth shopping experience for all users. This focus on customer satisfaction strengthens its reputation. https://www.jeem.pk/

@Sohailsfhfg
Copy link

Enjoy the ultimate volleyball challenge with The Spike Game. Designed for all platforms, it brings smooth controls, stunning graphics, and competitive gameplay. Whether you're practicing alone or competing with others, every match is filled with action. Join the fun now at The Spike Game.

@wikii4453
Copy link

wikii4453 commented Mar 12, 2025

These are useful .htaccess redirects for domain migrations, subfolder structuring, and query parameter handling. Be careful with rules to avoid redirect loops and unintended behavior. i Test it before applying site-wide. cookssimplepainting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment