Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created February 2, 2012 04:30
Embed
What would you like to do?
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]
@pitylee
Copy link

pitylee commented Feb 27, 2021

Hi, I am hoping to get some help. I am struggling with redirecting my urls which I have 84 to do.

Use htaccess redirect generator for bulk 301 redirect code generation. But add https://www to URLs for correct recognition.
As result, you will have

RewriteCond %{QUERY_STRING} ^id_product\=12&controller\=product$
RewriteRule ^index\.php$ https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls? [R=301,L]

or

RewriteCond %{QUERY_STRING} ^&*(?:id_product\=12|controller\=product)(?!.*&\1(?:&|$))(?:&+(id_product\=12|controller\=product)(?!.*&\1(?:&|$))){1}&*$
RewriteRule ^index\.php$ https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls? [R=301,L]

depending on query string match policy.

Also, possible to use PHP header 301 generator
You will have something like this:

if (isset($_SERVER['REQUEST_URI']) && parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == '/index.php' && isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] == 'id_product=12&controller=product'){header('Location: https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls',true,301);exit();}

That my fella, is the wrong solution for my idea.
Not to be followed, good practice is a comment above that.

@myckhel
Copy link

myckhel commented Apr 2, 2021

Hello,
I hope i can finally get help over here.

i want to forward all request from old path to new path and retaining the method and queries.

OLD URL PATH:

[POST|GET|PUT|DELETE]: mydomain.com/public/api?key=value

Desired URL PATH:

[POST|GET|PUT|DELETE]: mydomain.com/api?key=value

Please can any one help me with the .htaccess code to achieve above idea.

@sebasira
Copy link

sebasira commented Jun 13, 2021

HI I'm having troubles making a redirection, and maybe you can help me.

I have two domains in the same VPS: domainOne.com and domainTwo.com. I want to redirect every user that navigates to domainTwo.com to domainOne.com, and I have this redirection (in domainTwo.com) that works perfectly fine:

Redirect 301 / https://domainOne.com/

But now I'd like to redirect everything, except the main/home of domainTwo. I mean, I will have a landing page (index.html) on domainTwo root and I one to keep it, but redirect anything else.

Thank you!

@ratebalmsoty
Copy link

When Redirect with htaccess to link the browser show ?url= in the last of link

RewriteEngine on

Redirect /elitehomespro/projects/Real-estate-for-sale-in-the-center-of-Istanbul-with-high-investment-advantages https://www.almsoty.com/5-Best-Discord-Bots-for-Your-Business

RewriteRule ^projects/([a-zA-Z0-9-أ-ا-آ-ب-ت-ث-ج-ح-خ-د-ذ-ر-ز-س-ش-ص-ض-ط-ظ-ع-غ-ف-ق-ك-ل-م-ن-ه-و-ي/]+)$ project.php?url=$1 [NC,L]

@vinznsk
Copy link

vinznsk commented Sep 18, 2021

Thanks for the example. I'm trying to implement it, but there is a weird problem I have.

https://vegan4k.com/index.html - This page shows 404 and it doesn't want to redirect to the homepage and I don't understand how to make it work.

I'm trying to add smth like:

RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

But it shows 500 error. Can anybody help? Thanks!

@lightend
Copy link

lightend commented Oct 9, 2021

Hi, I need help because I am struggling with redirecting a broken link to my website page/url.

Please guide me how can i redirect this url: https://softlay.net/graphics-design/...rdu-fonts.html

To this: https://www.softlay.com/downloads/500-urdu-fonts

I am redirecting my entire old site (.net TLD) to new domain (.com TLD) and preserving url paths.

I think my problem is due to the URL having unsupported file extension (i.e "...rdu-fonts.html" instead of ".html")

I have tried "Redirect a single page" and "Redirect any file with the ...rdu-fonts.html extension" code snippets.

I hope someone may be able to help me. Thanks.

@vikasrajma
Copy link

Please help me as I'm having trouble redirecting a broken hyperlink to my site's page/url. https://www.avoid.uk.net/windows-11-download-iso/

To this https://www.avoid.uk.net/192-168-1-1/

@voicenoise
Copy link

Hello,
i changed my hosting from nginx to apache.

before i had my own cdn and cdn.domain.com are redirected to domain.com.

i try a few .htaccess redirects but always it ending with a redirect loop.
the cdn root is pointed to the main root.

is there a way to redirect the cdn.domain.com to domain.com?

thank you in advance

@mickel2022
Copy link

I don't want to manually redirect all addresses to this format .
I want all address /amp are
be guided backwards

Redirect /air/amp /air

https://jojhelp.com/

@gochi88
Copy link

gochi88 commented Sep 12, 2022

Thank you for this guide information, now I can redetect site https://relaxmodapk.com/ without any plugins, Thanks sir.🤞

@Jimmychohankhan
Copy link

I appreciate the information listed in this guide and will have no problem reidentifying https://queenapk.com/ without any plugins.

@apkzarcom
Copy link

Thank you for this guide information, now I can redetect site https://apkzar.com/ without any plugins, Thanks sir.
HOPE YOU HELP

@apkzarcom
Copy link

Thank you for this guide information, now I can redetect site https://apkyolo.com/ without any plugins, Thanks sir.

@apkzarcom
Copy link

now I can redetect site https://instamody.com/ without any plugins, Thanks sir.

@apkzarcom
Copy link

now I can redetect site https://apkrog.com/ without any plugins, Thanks sir.

@apkzarcom
Copy link

now I can site https://apkzar.co/ without any plugins, Thanks sir.

@apkzarcom
Copy link

hello this will help youmore https://nxtvillain.com

@trucaller12
This comment was marked as spam.
@marioaprea
Copy link

I would like to change document root path for sites installed in subfolder of main domain:
example
www.mydomain.com/site1
www.mydomain.com/site2
www.mydomain.com/site3

when call a page on
www.mydomain.com/site1
if would like that my document root for this site point to /site1 folder and not to www.mydomain.com folder

Thank you

@lisamathew221
Copy link

iBOMMA - Watch and Download ibomma Telugu movies from ibomma.com. Watch Telugu movies in an excellent quality at the smallest file size.

@lisamathew221
Copy link

If you’re unsure where to find a casual mate, try Doublelist app with lots of sugar babies and independent escorts. All girls are incredibly beautiful and open to experiments with a stranger.

@lisamathew221
Copy link

If you live in Berkeley, Ca., and are looking for a Berkeley moving company. Then look no further than the New Day Moving company. We are a leading Berkeley moving company and our clients are happy with our work. Call us anytime to book your schedule.

@lisamathew221
Copy link

John Patrick University's School of Integrative and Functional Medicine university offers degrees & certificates in nutritional health.

@getandroid
Copy link

It helped me a lot to redirect my website which is for download free vpn without any plugin

@allin211
Copy link

allin211 commented Jan 4, 2023

i have used all the redirects listed here including image expire headers https://findapkmod.com/

@Kiankhan01
Copy link

Download Most Mod Apk for Android ...
https://apkspicy.com/

@Kiankhan01
Copy link

If you want to download Kinemaster Pro, you can download it very easily from this goolge. We can also download it from play store. But we need no watermark and no ads. That’s why we prefer to download it from Google.

Download Kinemaster Pro Mod Apk

@deependrashilpi
Copy link

Thank you sir for such a valuable Information

@deependrashilpi
Copy link

Thanks for the information contained in this valuable Content

@caylakling
Copy link

A very excellent blog post.Thank you so much for such a well-written article. Slope Unblocked

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