Skip to content

Instantly share code, notes, and snippets.

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]
@TakaraDolphin
Copy link

TakaraDolphin commented Mar 15, 2020

I used to have a shopping cart as a folder inside my main website. It began having issues and I decided to abandon it for a different cart.
I need to redirect everything inside the www.MYWEBSITE.com/store/ folder to the main page of a different folder inside the same website.
PLEASE HELP!

@bea023
Copy link

bea023 commented Mar 18, 2020

Hello,

I'd like to create redirection rules according to the following needs:

OLD URLs:
www.mysite.com/lang1/old_slug
www.mysite.com/lang2/old_slug
www.mysite.com/lang3/old_slug

NEW URL (same for 3 langx)
www.mysite.com/new_slug

Can anyone help?
Thanks in advance

@ageorgesrv
Copy link

ageorgesrv commented Apr 24, 2020

Hi im have the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^srvgeo.com$ [OR]
RewriteCond %{HTTP_HOST} ^192.168.0.10$
RewriteRule (.*)$ http://www.somedomain.com/$1 [R=301,L]

The redirect is working good , but i would like to exclude that if im requesting to access to http://srvgeo.com/index.html i get to see this file instead of been redirect to http://www.somedomain.com/$1
Can some help me, please.
Thanks in advance for your time.

@prakharchhipa
Copy link

prakharchhipa commented May 12, 2020

Hello,

I'm using Github pages for a static website and DNS via Godaddy. I'm trying to redirect https://abc.xyz.com to https://xyz.com/abc. Not getting understand how to add subdomain redirection to sub-folder.

Both repositories are different "abc" and core one.

Is there a way to do it, can anyone help in this?

@bhaktisansar
Copy link

bhaktisansar commented Jun 20, 2020

@pitylee
Copy link

pitylee commented Jun 21, 2020

RewriteEngine On

Redirect 301 /chalisa/hanuman-chalisa-with-lyrics/ /

I think

@jasmine124
Copy link

jasmine124 commented Jun 25, 2020

Hi, please can you help with this redirection:

https://www.domain.com/index.php/component/users/?view=remind&Itemid=109
to: https://www.domain.com/

What I don't follow is why even this line isn't working:

RedirectMatch 301 "https://www.domain.com/index.php/component/users/?view=remind&Itemid=109" "https://www.domain.com/"

@jonasmarco
Copy link

jonasmarco commented Jul 7, 2020

I just have to thank you, it helped me a lot. :)

@vineetchauhan-vch
Copy link

vineetchauhan-vch commented Jul 17, 2020

Dear Friends ,

Please help me to write the below querry for 301 redirect .htacess (Apache Server ) , removing www & a Folder in between at a same time

I want to redirect the below old url :

https://www.mydomain.com/tour-package/india/rajasthan

to new one

https://mydomain.com/tour-package/rajasthan

Thankyou

@ysselst
Copy link

ysselst commented Sep 8, 2020

How can I force every visitor of my site to go to the only one preferred URL: https://sitename.com?
Now Google Speed Insight says there are the following redirechts:

(Initial: http://sitename.com/)
http://www.sitename.com/ | 190 ms
https://sitename.com/

Thanks for help!

@bhaktisansar
Copy link

bhaktisansar commented Sep 11, 2020

@vasilestefirta
Copy link

vasilestefirta commented Sep 30, 2020

@bhaktisansar you can try to use this RewriteRule in your .htaccess file:

RewriteEngine on
RewriteBase /
RewriteRule ^ac-services-in-gurgaon/?$ / [R=302,L]

This rule will match both /ac-services-in-gurgaon/ and /ac-services-in-gurgaon. If you want a permanent redirect instead of a temporary one, then use 301 instead of 302.

You can also use the .htaccess testing tool: https://htaccess.madewithlove.be/

@nathanpuls
Copy link

nathanpuls commented Oct 12, 2020

I wrote a little article called "Using .htaccess to forward people to “Today’s” reading." It was for a devotional our pastor wrote. I had to make it so that each day would forward to that day's devotional. It was a neat project. https://medium.com/@nathanpuls/how-to-set-up-sms-text-reminders-for-yourself-with-gmail-51380d3af010

@uearnclick
Copy link

uearnclick commented Oct 26, 2020

Hi, is it possible to create a 301 redirect for the following URL string

oldpage.php free udemy Course available 100% off100% off udemy Course
And problem in uearnclickCyber security tutorial

@labeshops
Copy link

labeshops commented Jan 30, 2021

I cannot make this work.

I have a testing site of my cart software located in mysite.com/beta/v3 that somehow google indexed and were placing orders on that I knew nothing about.

So I am trying to redirect /beta/v3 to the same page on the correct domain, though I use a multistores setup so all my files are at mainstore.com though the same installation also shows as secondstore.com thirdstore.com etc which are in effect virtual domains. I cannot just redirect all /beta/v3 links to / as it would not take them to the correct store. So I need to put in the right domain name.

My cart software (opencart 3.0.3.2) uses {SERVER_NAME} as a variable in htaccess so I tried:

#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} ^/beta/v3/(.)$
RewriteRule ^(.
) https://%{SERVER_NAME}/$1 [R,L]

but this doesn't work.

@bebo3xd
Copy link

bebo3xd commented Jan 31, 2021

Hi,
Could anyone help me with this?

Original URL:

https://domain.com/en/shop/locations?=name&....

i need to redirect it to the same everything i just want to change /shop to be:

https://domain.com/en/resturants/locations?=name&....

i have tried most of the examples above but no one worked to me..

Thanks.

@singhvikramjeet786
Copy link

singhvikramjeet786 commented Feb 4, 2021

Hi, what rule could make this work for me?
All URLs may carry queries in case trackers are placed
e.g. https://domain.biz?tracker_id=1

URL: https://domain.biz
ACTUAL PAGE: https://domain.com/folder/subfolder/page1
DISPLAY: https://domain.biz/

URL: https://domain.biz/any_other_page
ACTUAL PAGE: https://domain.com/folder/subfolder/any_other_page
DISPLAY: https://domain.biz/any_other_page

I was working with this (but it doesn't work).
RewriteCond %{HTTP_HOST} ^domain.biz$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.biz$
RewriteRule ^(![^/]+)?$ /folder/subfolder/page1/$1 [P]
RewriteRule ^(.*)$ folder/subfolder/$1 [NC]

I tested this in https://htaccess.madewithlove.be/ but doesn't give me the desired results.

Any help please?

Thanks!!!

Did you found the answer to this? as I'm also having the same issue.

@aptol1952
Copy link

aptol1952 commented Feb 5, 2021

Hope you can help. I have a long url which needs to go to a shorter one. there are many page associated with the original that should go to the new one, like wildcarding. Cannot seem to make this work.
origin is
https://malawitravel.org/discover-malawi/african-national-parks/parks-southern-region/regions/african-national-parks/southern-region/...(page.html)
New is:
https://malawitravel.org/discover-malawi/african-national-parks/parks-southern-region/...(page.html)

Origin / New are many pages with .html

Hope to hear from you and thank you in advance!

@jack1133
Copy link

jack1133 commented Feb 25, 2021

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

3 example are as below -

tillrollsonline.co.uk/index.php?id_product=12&controller=product https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls
tillrollsonline.co.uk /index.php?id_product=14&controller=product https://www.tillrollsthermal.co.uk/single-ply-paper/44x70mm-a-grade-till-rolls
tillrollsonline.co.uk/index.php?id_product=13&controller=product https://www.tillrollsthermal.co.uk/till-rolls/44x80-a-grade-till-rolls

I am going from tillrollsonline.co.uk to tillrollsthermal.co.uk

I think my problem is due to the URL having index.phpin?

I hope someone may be able to help me.

Thank you in advance.

@pitylee
Copy link

pitylee commented Feb 25, 2021

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

3 example are as below -

tillrollsonline.co.uk/index.php?id_product=12&controller=product https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls
tillrollsonline.co.uk /index.php?id_product=14&controller=product https://www.tillrollsthermal.co.uk/single-ply-paper/44x70mm-a-grade-till-rolls
tillrollsonline.co.uk/index.php?id_product=13&controller=product https://www.tillrollsthermal.co.uk/till-rolls/44x80-a-grade-till-rolls

I am going from tillrollsonline.co.uk to tillrollsthermal.co.uk

I think my problem is due to the URL having index.phpin?

I hope someone may be able to help me.

Thank you in advance.

!!! Good solution

And what is the htaccess you are trying to use?

Because this is a gist about htaccess, here is how you do it with htaccess:

Redirect 301 index.php?id_product=12&controller=product https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls

I think it would be much easier, if you would store a csv or a php array in the index.php, and you did something like:

$redirects = [
    12 => 'https://www.tillrollsthermal.co.uk/till-rolls/a-grade-paper-till-rolls/37x70mm-a-grade-till-rolls',
    13 => 'https://www.tillrollsthermal.co.uk/single-ply-paper/44x70mm-a-grade-till-rolls',
    14 => 'https://www.tillrollsthermal.co.uk/till-rolls/44x80-a-grade-till-rolls',
];

if( 
    isset($_GET['id_product']) &&
    in_array($_GET['id_product'], array_keys($redirects)) &&
    is_numeric($redirects[$_GET['id_product']])
){
    header('Location: ' . $redirects[$_GET['id_product']]);
}

I would do it with javascript or html redirect tho, since it is nicer if you redirect it in time, and show a pop-up on the old website, than redirect the user to the new website, and in the meantime tell the Google some canonical url's and schema info, plus tell it to not follow the link, and what is the new link in place; it is a good seo practice and internal backlinking.

@bond7071
Copy link

bond7071 commented Feb 26, 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();}

@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

ratebalmsoty commented Aug 22, 2021

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

vikasrajma commented Apr 2, 2022

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

voicenoise commented May 27, 2022

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

mickel2022 commented Jul 3, 2022

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/

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