Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Common .htaccess Redirects
#301 Redirects for .htaccess
#Redirect a single page:
Redirect 301 /pagename.php
#Redirect an entire site:
Redirect 301 /
#Redirect an entire site to a sub folder
Redirect 301 /
#Redirect a sub folder to another site
Redirect 301 /subfolder
#This will redirect any file with the .html extension to use the same filename but use the .php extension instead.
RedirectMatch 301 (.*)\.html$$1.php
#You can also perform 301 redirects using rewriting via .htaccess.
#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*)$1 [R=301,L]
#Redirect to www location
RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^ [nc]
rewriterule ^(.*)$$1 [r=301,nc]
#Redirect to www location with subdirectory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} [NC]
RewriteRule ^(.*)$ [R=301,NC]
#Redirect from old domain to new domain with full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*){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 ^(.*) [R=302,NC]
Rewrite and redirect URLs with query parameters (files placed in root directory)
Original URL:
Desired destination URL:
.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:
Desired destination URL:
.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:
Desired destination URL:
.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:
Desired destination URL:
.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:
Desired destination URL:
.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 ^(.*)$$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 ^(.*)$ [R=301,L]
#From ->
retains path and query, and eliminates xtra blog path if domain is
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*){REQUEST_URI} [R=302,NC]
RewriteRule ^(.*){REQUEST_URI} [R=302,NC]

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

Original URL:

Desired destination URL:

ezpaider commented Apr 2, 2014

@purse9644, in order to redirect from your original URL to the new desired destination, you can do the following:

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)/old-directory/(.*)$ $1/new-directory/$2 [R,L]

Is it possible to redirect from /example/ to /example/index.php?
We are using Disqus as a blog engine and it is very picky about matching exact urls.

Netzie commented Jul 11, 2014

Can you help me with moving old URL/Domain to new domain
Without +FollowSymLinks that my host don't want/allow and I want to catch all subdomains and files
So if a request comes for
it will be send to

I tried most of the examples above and results in this arbejdhjemme.comstart-here (no slash) :-(
Thanks for your time and help

Hi, do you know how to redirect always (in all subfolders) from to

Great examples, but I have one a little more complex that I can't figure out. Old website had blog entries in New site is just For example: needs to redirect to

Any help is greatly appreciated.

How to redirect from a folder into that folder's subfolder ? ->

Thanks man, this just saved me a whole bunch of time!

Is it posible to redirect from to
What I want to get is that when I go to it automatically goes to, without changing the url on the browser. Is that feasible?
Thanks a lot!


ghost commented Jan 20, 2015

I'm attempting to redirect URLs that have multiple parameters for example: to a clean URL on another domain like

Can anybody give me a few pointers please?

Hey, how can i redirect old domain to new domain while retaining the path after the old domain name. for instance to

Dear Friends,
I just want to redirect this page in a proper category page. so please give me a guide for that..Page is mention bellow, please follow as..
I look foreword to your response quickly.....

This is a terrific post. Just wanted to add: if you have a Wordpress site with a permalink htaccess directive already in place, AND you wish to do a 301 from to, then you need to do this:

Redirect 301 /

Adding the "?" will add a "?" at the end of the redirect URL. This will force the browser to view anything after the "?" as parameters. Otherwise, the permalink redirect will "break" your 301 redirect. And if you remove the permalink redirect, you're going to break the 301 redirect.

I found this out the hard way by banging my head against the server for like a week.

Thank you very much for this incredible helpful gist :) everything I need to know about .htaccess files in one place 👍

I have a site using permalinks. and it works perfect. aka and the akin permalink

the above works great. Now on a different server with a different site root. I have it so were a person to come using they see the mobile site. this all works fine but then I want to use a perma link for this. thus. This site which has its own site root. I have added

Redirect 301 /cartypes.php?cartype=12

the above gives me a 404 yet were i to go to /cartypes.php?cartype=12 I see the related page.

I have tried everything and been to a thosand webpages looking to resolve this. thus am very grateful for any input your side.


I need help redirecting from one subdomain to another while maintaining same path({REQUEST_URI}
i.e all requests with this context path (pv5/v8/1)
should be redirected to

Can anybody help?
I want like this.
But without hard coding the host. so that this will also work

Just wanted to add:
If you are working with WordPress, add your redirects ABOVE WordPress rules

i m working with core php
my url is like

i want to make it like

Here's what I'm having a problem with... redirects to - Great...this is what I want.
However, redirects to - This is no bueno. I just need it to add the www., so it will still go to the /blog/ WPMU site.

Is this an .htaccess thing? Thanks in advance!

Hi All,
I want to redirect to

Any help is greatly appreciated?

fbayer commented Sep 25, 2015

Files moved to another directory: test-a >> test-b
Old links to any file within the test-a folder have to be redirected to the new folder - test-b.
should be redirected to

The htaccess file is located in the root of the web project and works fine for other rewrites, but not for this one:
RewriteRule ^/Test-A.htm?variable=1&pathvar=tests/test-a/(.*)$$1 [L,R=301]

Does anyone see the problem? Thanks in advance.

pauly69 commented Oct 7, 2015


Google showing Duplicate Title and description Tags Errors in GWT under HTML Improvements duplicate meta description - 4 and duplicate title tags - 4.



So, I need search engines not to index the ?m parameter at the end of my website url. I guess this is a development issue. In the .htaccess I found the code below that might lead to the m parameter issue on google. Does someone know how to fix it?

Redirect urls based on a query string parameter

RewriteCond %{QUERY_STRING} ^(.&)?view=mobile([^&])&?(.)?$
RewriteRule ^(.
)$ /$1?m%1%3 [CO=is_mobile:1:%{HTTP_HOST},CO=disable_mobile:0:%{HTTP_HOST},L,R]

RewriteCond %{QUERY_STRING} ^m_(.&)?view=full([^&])&?(._)?$
RewriteRule ^(.*)$ /$1?%1%3 [CO=disable_mobile:1:%{HTTP_HOST},L,R]

I am also confused because my mobile version is /mobile and not m? so I don't understand how Google managed to index the m parameter.

Also when I inserted the canonical url on the /mobile version and the alternate url on the desktop version google mobile search delivered the following (mobile-friendly) (mobile-friendly)

but before the changes: (mobile-friendly) (mobile-friendly)

So this tells me that google detected the mobile site regardless of the changes I made after. As you can see it says Mobile-Friendly ( So if it wasn't for the ?m parameter I would not have any duplicate content. So how am I to fix this? It is a nightmare.

criboats commented Oct 8, 2015

Looking to redirect the following:

I'm stumped.

Please and thanks in advance!



I have two below 301 redirects on my htaccess file.

Redirect 301 /test/sample /newurl
Redirect 301 /test/sample/redirect /subpage

When I tried to check /test/sample/redirect, it should be redireced to /subpage however, it is being redirected to "newurl/redirect" which leads to 404 page. I have tried with changing the order as well on the htaccess file, but no luck.

Could anyone please help me to solve this. Thanks in advance.

Hi, first of all, congratulation! Very helpful info.
I need an advice, how to redirect to the last category in the path. For example: to
Doesn't matter the number of categories, I want to redirect to the last. I successfully create a redirect but I want to create an universal rule for all categories, not to describe one by one. Is it possible?

So, my conclusions are that can't create an universal rule. The rule which i use is OK,

RewriteRule ^(.)/(.)$ /$2 [R=301,L]

but also redirect the other resources like css, js, jpg etc. I use three rules to do what I want:

RewriteRule ^my-category/(.)$ /my-category [R=301,L]
RewriteRule ^(.
)/my-category$ /my-category [R=301,L]
RewriteRule ^(.)/my-category/(.)$ /my-category [R=301,L]

It will be great if somebody give some optimization.

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

oldpage.php // / newpage.php

Kazzje commented Jan 21, 2016


What if I want to reach this: redirecting to

How can I write a condition?
Thanks a lot!

How to redirect to . I have .htaccess in root folder and sub-folder (abc) as well. How and where should i write condition?


Hi Scott. This is a great gist, thank you! I'm hoping you can help me do the following and I apologize if it's already been answered in your post or the comments, I'm not very familiar with Apache and am not always sure of what I'm reading.

I want to redirect to - I've got that part by using this:

RedirectMatch "^" ""

However, I'd also like to have requests to not be rewritten/redirected. Currently any attempt to access a subdirectory gets redirected to

Can you please advise?

Hi Scott,

Can you please help me to stop https redirect for a particular url, Please find my current .htaccess


RewriteCond %{HTTP_HOST} ^ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$$1 [L,R=301]

I want below url to not redirect on https{ID}

basically i want urls not to redirect which are including "/setup/"

Any help will be much appreciated
Thanks :)

tripledm commented Jun 9, 2016

Hi Scott,

I have a tricky redirect and hoping you or someone can help me.

The redirect I need must remove both numbers and ".html", so all the URLs with a number at the end.

Original URL:
New URL:

Thank you in advance!

dduboy commented Jun 12, 2016

Hi !
I have a main domain website with its own .htacess working well. This main domain is made with WordPress, under the path httpdocs/ and all is working well. I have also some subdomain : on my server that i don' t need anymore, but i don't want to loose the rank and links. So i would like to redirect them permanently to the main domain's corresponding page or virtual folder.
The subdomain is under this path on the server Apache (i didn"t choose this, it is done automaticaly when i create it throught plesk : subdomains/my-subdomain-A/httpdocs/
It has no files Inside the directory at all, just some virtual categories and subcategories folders, and everything is in php.
So i tryed this and i've sent the .htaccess file in the "subdomains/my-subdomain/httpdocs/" root directory httpdocs. But no way...does'nt work.

Options +FollowSymlinks
RewriteEngine on
RedirectPermanent /afolder/page1.php
RedirectPermanent /page2.php
RedirectPermanent /page3.php

When i use this method for redirectpermanent, it does'nt work (error 500). I have searched for some ideas everywhere, but Nothing seems to work...any help will be much appreciated !! Thank you in advance !

current url:

Expected url:

what is the code i have to use can u suggest me??

anavasis commented Jul 4, 2016

Can you please help me in making some redirects, i will mention them below: to to
all sites having /index.php/ like to be redirected to
also all sites having /index.php/ like to be redirected to

please give all the text i have to put to .htaccess because i have no knowledge about the specific purpose

I'm a little bit confused about these URL REWRITE rules so I'll ask directly my need ;)

It's a little bit non common but I would like to REDIRECT everyone going to the "/" path to a straight forward but URL REWRITE the rest of the URL if filled (a subfolder + file like "/subfolder/XXX.YYY" or even only a file like "/XXX.YYY")

Could you help me figure it out please? :)

I am little bit in trouble here . don't have a host but have a domain and also wanna redirect this domain to another domains subdomain. Is it possible to achieve this without using .htaccess?
let me be a little more clear.
i want to redirect to .
While I am doing this i want the url shown as entered like this. For example when I enter I want it to show the contents from

Can you please direct me to the right direction?

mdehaan commented Jul 22, 2016 edited

How to Redirect specific page on specific domain alias to https/SSL.

The webhosting is setup with multiple domain aliases on the same vhost. It needs to redirect a specific page on a specific domain alias to https. needs to redirect to

The other domains, and don't need to redirect /contact to https

Can you please help me what code to write in .htaccess file...
How can i redirect from to
I want to redirect my whole website pages to my new websites's home page..
Please help me...

Thanks in advance

patelhardik1011 commented Aug 1, 2016 edited


can you please help me to resolve my below issue :

I have one domain named Currently I am accessing its admin url using this url :

I want to access admin url using this url : I have tried some examples but they are redirecting me to this URL. But I want to show this in address bar.

I have project in CakePHP 3.x. So Can you please tell me that how can I achieve this functionality ?

Thanks in Advance.

Hi Guys,

I want to redirect few folders and subfolders from one domain to another domain with different folder names (page names), first level pages are redirecting bu on second level I get 404 .
anybody can help?

Hi guys, so I've got a wordpress site which looks like:

and I need to redirect all pages sharing that same root URL to:

How do I do this without going in and adding manual redirects to every single page?

issuru commented Sep 1, 2016

GuidoSchmidt commented on 23 May 2015

Thank you very much for this incredible helpful gist :) everything I need to know about .htaccess files in one place 👍

Exactly. Cheers

twpeach commented Sep 6, 2016


Thanks for this great article. But my situation is a little bit more complex.

Do you know how to redirect the following urls:


Which comes with different domain name & different post id though! Thanks a bunch!!

dbaja70 commented Sep 8, 2016

i have to redirect old link
to new page

I tried with rewriterule:
RewriteRule ^kontakt.php?lang=(sr|en)$ /index.php?lang=$1#CONTACT [NE,R]
but, as result, i get this:

Why rewriterule change order of parameter lang and anchor #CONTACT???

I've been playing with this for a week with no luck, maybe you can help me. I want to 301 Redirect......[number]

Would greatly appreciate any help.

hii hope anyone can help my problem is i have this rule in my htaccess with some other rules

RewriteEngine On
RewriteRule ^work(.)/([^/]) work-single.php$1?slug=$2 [QSA]

what it basically does that it takes the slug value and pass it to the work-single.php . So my only one page work-single.php is used to show my all projects. work is the virtual directory which does not exist in real but it is added on url

what I want to do is like when ever any user enter like ex -[ ("upasna-group-self-branding") this will change from project to project it is comming from other page or database ] I want them to redirect to

and when they enter like or i want them to redirect back to

please help me to write this

atlarep commented Sep 28, 2016

I want redirect when people try to browse through the /includes/ sub-directory of my website (where the java scripts and config files are), but when I do it, all the java scripts don't work on the site. Is there a way to do this without breaking java script?

mpourkos commented Oct 8, 2016

Redirect to different domain.
current url:

Expected url:

what is the code i have to use can u suggest me??

imuneer commented Oct 25, 2016

Excellent piece of script set

Only this worked for me with wordpress

  • redirect all traffic from old domain to new domain, in same hosting
  • new domain is add-on domain
  • wordpress is installed in folder (not in root)
  • placed on top of the .htaccess in yourblogfolder/ (the wordpress installation folder)
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^(.*)$ "http\:\/\/new\.com\/yourblogfolder\/$1" [R=301,L,QSA]

alankeys commented Dec 6, 2016

Question about 301 redirect / modrewrite

Pleas I would apprec iate any help in you understand mod rewrite...
So far I have not managed to create my needed redirect.

here is my current URL :

needs to redirect to :

so am adding 'id' and renaming the variable 'title'.

does anyone know the setting for this ??

i want to redirect page

manual enter 1 url then auto redirect main domain and 2 url.

It looks its good post for my problem, I have website hosted in openshift for instance and this will list out all the available products but if query param added to the above url like this, it displays only one product and everything working fine. But I have created domain in godaddy for instance and forwarding to which is working but when I add query param to, its not working. What is the problem and what should be done in order forward the request to from domain with query param

Try adding (.*) at the end.

For some reason when i add this 'redirectMatch 301 ^(.*).html $1' to .htaccess i get:
{baseurl}/index/ for the home page. But everything else is working fine though.
Why is the happing?

chancegehrke commented Jan 24, 2017 edited

I'm trying to figure out how to do this type of redirect:

change this:
to this:

Any suggestions?

I'm basically trying to just reverse this one:
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]


I want to redirect my old domain each every pages to my new domain


How to do this kind of redirection

lhesky commented Mar 6, 2017

Hi Sir,
I am trying to write a htaccess in mod_rewrte

i have a search textbox in default .php and redirect from jquery in window.location

i get the parameter variable from search.php

i used this rule but i cannot get the way i want below
RewriteRule ^search/([^/.]+)$ search.php?strname=$1

to look this way. Please help me.?

Thanks for this! Huge help!

How to use [L] transparent to make a redirect a directory file url be in the site index



To be

goatcloud commented Mar 31, 2017 edited

I have a bit of a curious one. I had a former WordPress website, let's call it Full blown site, with pages, posts, categories, tags, etc.

I want to redirect every single URL from to

I set up a site redirect at the domain registrar to do that, but now I get many 404s upon redirection, like or or and the like

I'm not sure if I can use the .hatccess file at to accomplish this or if I have to set something up at the registrar for

Any ideas?

Solved my own problem. Maybe this will help someone else:

  1. Removed the redirect on at registrar. Set the nameservers for to a place I control.
  2. Created an "add on" domain for via cPanel.
  3. Created an .htaccess file at the "holding" add-on domain for the old domain and added the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^/?$ "" [R=301,L]

ErrorDocument 404

Possible to do redirect for ->

in both cases, the root directories are same, need to redirect to a subdomain without affecting the project execution.

In cakephp 3 is Possible to redirect. to

jphoke commented Apr 17, 2017

Having a bit of trouble working this out .

Moving this to

I want to redirect any /subdir/ path including anything passed via GET string to the newdomain



Thanks in advance

ruifct commented Jun 3, 2017

hi guys, how can I redirect all the pages of to, keeping the same url structure? The site is already copied on the new domain. Thanks

I would be grateful for any input, I have the following in my htaccess file

this is to force to index file rather than just it will give

RewriteEngine On
RewriteBase /

#only for the root directory
RewriteCond %{REQUEST_URI} ^/$
#if the uri is not already index.php
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule ^$ /index.php [R=301,L]

the above works great, when ever a punter goes to it directs the punter to

I have an issue thought
The site is multi lingual, and I have also

how can I force were someone to just type to force going to

Thank you for looking anyway lol


dupls commented Jun 26, 2017

I have an entire website that I want to redirect to a new website folder.
#Redirect an entire site:
Redirect 301 /
Which works, BUT, if people have an old link the first part is replaced but the rest of the link is added to the end, which of course doesn't exist anymore. I want to make sure any old bookmarks resolve to the regardless of what they are.
I also tried.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}.txt(?:\ Comodo\ DCV)?$
RewriteRule ^phprimer.*$ [R=301,L]
RewriteRule ^/?$ [R=301,L]
Again the first part of the rewrite works so the path is but since the rest of the path is tagged on and doesn't exist I get 'Page not found'.

Any direction would be appreciated.

Zuz66 commented Jul 4, 2017


I have a small problem I hope someone can help me

I redirected all articles made in wordpress from

/%category%/%post-name%/ to /%post-name%/

to make this i use this in htaccess

RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/]+)/$$3

all is fine, categories url are rispected, but i have problem with pagination

when category is like
clicking on results are wrong

instead is ok with
clicking on result is correct is goes on second page of category

how can i solve?



how can i redirect all links of my site :

both and mywordpressfolder has separate WordPress installed.
should i write rule in mywordpressfolder .htaccess file ?? what rule it should be ?

I am helpless as of now !

Thanks in advnace

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