Batch RewriteRule Generator
- Comments:
- 198
I find myself often needing to set up a large number of 301 redirects from an excel file of old to new url’s. Until this point, writing these had been a fairly exhausting process as you need to be certain to escape every character that could be picked up by the regular expression engine, or risk unintended consequences. On top of that, writing these rewrites to catch GET parameters in any order is complex to say the least. While working through a large list of 301s for a very picky server where Redirect 301 style redirects were not an option, I began setting up RewriteRule's by hand and decided there had to be a better way. Voila! My 301 RewriteRule Builder was born.

You can simply enter a list of tab or space delimited urls (copied from Excel works great) and it will spit out the generated RewriteRule’s for you. GET parameters on the old urls are broken up and built into multiple RewriteCond’s set up to work in any passed order.
I hope someone finds this useful. Any comments are welcome below, of feel free to fork me on github!
Please make sure you have RewriteEngine On
somewhere near the top of your .htaccess, before you attempt any rewrites.
Need more help? The tool can only do so much, and sometimes you need human help. I offer .htaccess help as a paid service and have 10+ years of Apache experience. Send me a note on my contact page and we can discuss your options.
Comment by: Dan Shields on
Thanks so much!!
Comment by: Chandra on
Comment by: Mike on
Comment by: Anton Evers on
Comment by: Anton Sombetzki on
Comment by: Alex on
Comment by: Sagan on
Comment by: Eric on
Comment by: Nathan Gaw on
Comment by: Eamonn on
Comment by: David on
I love that it does bulk processing, but I'm way more impressed with the fact it can decipher irregular characters. It's exceptional!
BRAVO!
Please contact me, and I'll send you a free graphic t-shirt from GAFY.com!
Comment by: Matt on
Comment by: Andy on
Does it go at the bottom of the file? top? and how does it work so the rest of the urls are not skipped please?
Cannot find any info for this when using a htaccess methodnad need to find a working method fast as website now transferred to brand new domain.
Comment by: Jesse G. Donat on
The order actually shouldn't matter at all when using rewrites this creates as it strictly matches.
Entering the following for redirect
http://foo.com http://bar.com
Returns the following.
RewriteCond %{HTTP_HOST} ^foo\.com$
RewriteRule ^$ http://bar.com/? [L,R=301]
The ^$
means [start][finish] and indicates that this only strictly matches empty so it won't bleed into other matches.
As such you will also want one for index.html/index.php/whatever the real url for your homepage is.
Comment by: David on
I've saved a lot of time! Thanks for the hard work.
Comment by: Hetal Shah on
Comment by: Luke on
Comment by: Philippe on
Comment by: Samantha on
Comment by: saravanan on
RewriteCond %{QUERY_STRING} (^|&)where=Oakland%2C\+CA($|&)
RewriteCond %{QUERY_STRING} (^|&)keyword=Beauticians($|&)
RewriteRule ^results\.php$ /results.php/k/Beauticians/Oakland/CA? [L,R=301]
Comment by: Helias on
Comment by: Helias on
Comment by: Ivan on
Examples:
1: www.asdasd.com/anuncios/buscar/1/?cat=190&buscar_anuncio=Buscar www.asdasd.com/inmuebles/
2: www.asdasd.com/anuncios/buscar/1/54/ www.asdasd.com/gibraltar/
Number 2 works with redirect 301 /anuncios/buscar/1/54 asdads.es@/gibraltar
Does anyone can help me?
Thanks for help :)
Comment by: Jesse G. Donat on
Comment by: Ivan on
Comment by: Jaime on
Do you know if it´s possible to make some kind of redirection specifying not only the path but also the domain of the old urls?
I need it because I am moving from a Joomla multi-domain website to a Wordpress multi-domain website and I have some identical paths in the old website like these:
www.doamin.com/news.html
www.domain.fr/news.html
So a simple redirect like below always send me to the .com (even in the french scenario) which is the main domain in the multi-domain hosting:
Redirect 301 /news.html http://www.domain.com/category/news/
Redirect 301 /news.html http://www.domain.fr/category/nouvelles/
At this moment the old site is at the ftp root and the new one is in a production subfolder but I have to delete the oldsite and move the new one to the root.. I would need to have the htaccess managing the redirections of all the pages from the oldsite to the new one. I have no problems with the old urls with different paths but, what can I do with the urls with the same path?
I only have one htaccess, since both domains are mangaged from a single CMS installation. (Old joomla site managed the domains with Joomfish plugin and the new wp site manage the domains with WPML).
Thank you!!
Comment by: Greg on
Comment by: Lorri on
Comment by: Dave Reeder on
This is the only tool that came up with a working redirect for some 404s I had.
Comment by: Evert on
I love those simplicity tools that are doing what there are designed for.
Comment by: Umal on
Comment by: kikos on
http://www.immofind24.de/index.php?controller=pjListings&action=pjActionMap
and i want
http://www.immofind24.de/immobilien-karte.html ..The rewrite is working but the form doesent show anything ..and a field ist already selected with a value ...any ideas ?
Comment by: Russ on
Comment by: Joe on
How could one modify it slightly to do the following?
I would want to remove the http host condition as I set the first redirect to push the www to non www version of the site. That condition is already met when I get to my 301's so I don't include it anymore on the individual ones.
Second, how can the output be set the RewriteRule starts with ^(.*)$ instead of the previous html page? in many cases any html page can generate the url so this accounts for all of them?
Again, simply amazing... even without my anal retentive tweaks.
Comment by: Paul on
Comment by: Mohsin Khan on
Comment by: Ashley on
Comment by: Marshall on
This is so incredibly awesome that it goes right past being incredibly awesome, goes all the way around, and comes back to being incredibly awesome.
And you. You're equally incredibly awesome for making it.
Wow.
Comment by: dutch guy on
Great one, cheers!
Comment by: David @ eltallerdelbit on
Like to use it to make my redirections and rewrites ;-)
Thanks!
Comment by: Amy on
Comment by: Mariel on
If I can enter a friendly url and it create the correct 301 redirect.?
Comment by: damian on
Comment by: Angela Tucker on
Comment by: Adrian on
Comment by: Alex on
# 301 --- /shop/dekorativnye-plenki/category/32-karbon?route=product/category&path=25_32 => /dekorativnye-plenki/karbon
RewriteCond %{QUERY_STRING} (^|&)route=product/category($|&)
RewriteCond %{QUERY_STRING} (^|&)path=25_32($|&)
RewriteRule ^shop/dekorativnye-plenki/category/32-karbon$ /dekorativnye-plenki/karbon? [L,R=301]
But as a result my server redirect me non existed page
/shop/dekorativnye-plenki/category/32-karbon?route=product/category&path=25_32
What is wrong?
Comment by: Sumesh on
.htaccess file with 1000s lines of redirects, need to convert these into a table data
Comment by: karpel on
http://www.site.ru/index.php/images/images/file/images/images/index.php /test/return not correct.
Comment by: Jesse G Donat on
http://www.site.ru/index.php/images/images/file/images/images/index.php http://www.site.ru/test/Update: As this is a common source of error for people I've updated it to better handle FULL→Relative URIs.
Comment by: Alex on
Lifesaver!
Comment by: Justin on
Will this work for http > https ?
example...
http://domain.com/cart/ to https://domain.com/cart/ ?
Comment by: Manish on
EX: http://domain.com/sub directory/
http://domain.com/sub\ directory/
Comment by: Reno on
Comment by: Christian on
Let the comments on to make debug easier later!
THANK YOU!
Comment by: Alpakka on
You need basic understanding on nginx blocks (to avoid duplicates etc with your existing configurations), but I quess you have that if ur working with nginx.
Just did around 100 Rewrites combining these 2 tools. Amazing.
Comment by: Jorge on
I'm trying to get rewrite but don't work for me. Can you help me. Now I have 6000 Redirec 301 and I would like to convert into rewriterules.
Comment by: Alex on
Comment by: Imran on
I'm about to use this to redirect 9000 .asp pages to static HTML links which are all Magento pho.
Will this work with asp pages as the old page:
E.g.
Www.oldpage.com/store/product.asp=1234
To
Www.newpage.com/bed.html
Comment by: Lesscro on
Comment by: Some dude on
Comment by: Jesse G. Donat on
It's out of scope of what this is meant for.
If you were interested, I offer htaccess help as a paid service. You can contact me via my contact page and we can discuss options.
Comment by: viktor on
RewriteCond %{QUERY_STRING} (^|&)id\=x($|&)
RewriteRule ^page\.php$ /page.php\?fid=x [L,R=301]
?fid=x
-> \?fid=x
Comment by: Jesse G. Donat on
?
.
Comment by: Phil on
its possible todo this?
https://subdomain.domain.tld => https://subdomain.domain.tld/subdomainfolder?
Thank u
Comment by: Dimitri on
when redirecting to the same url than this line should be skippet, this should not be possible:
# 301 --- https://testsite.com/contact => https://testsite.com/contact
RewriteRule ^contact$ /contact? [L,R=301]
We get a lot of list with somtimes redirects that do no need redirecting. than this check would be good.
Comment by: Tom on
# 301 --- a.php => b.php?filter%5B%5D=somevalue
RewriteRule ^a\.php$ /b.php?filter%5B%5D=somevalue [L,R=301]
The problem is, %5 doesn't make it to the final URL, when Apache rewrites it so this redirect is unusable. It's probably because '%' is a special character, I'm not sure. Flags B and NE don't help. Only thing that helped me was to decode that URL so I got this: b.php?filter[]=somevalue
Btw I created a similar tool http://redirectgenerator.netmagnet.cz/ :)
Comment by: Kevin on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^colorado\-renters\-rights\-law\.html$ "https\:\/\/domain\.com\/colorado\-renters\-rights\-law\/" [R=301,L]
Thanks again! What a great tool!
Comment by: Remo on
Comment by: Sreenivas Reddy on
TIME-SAVING TOOL
Thank you
Comment by: Al on
text/faq.html?faq=20#abc helpdesk/kb.php
# 301 --- text/faq.html?faq=20#Ben => helpdesk/kb.php
RewriteCond %{QUERY_STRING} (^|&)faq\=20($|&)
RewriteRule ^text/faq\.html$ /helpdesk/kb.php? [L,R=301]
I tried the following and it did not work.
RewriteCond %{QUERY_STRING} (^|&)faq\=20\#Ben($|&)
Any help would be greatly appreciated.
Comment by: Jesse G Donat on
Comment by: Michael Rock on
Comment by: Mike Lopez on
Comment by: Jason Schaeffer on
Comment by: viktor on
RewriteCond %{QUERY_STRING} (^|&)id\=x($|&)
RewriteRule ^page\.php$ /page.php\?fid=x [L,R=301]
?fid=x -> \?fid=x
</dive>
<code>?</code> - it is necessary to screen since it is a special character, this is certainly not superfluous.
<code>=</code> you are screening although here it never leads to problems, the special symbol is not.
https://httpd.apache.org/docs/2.4/rewrite/intro.html
<code>?</code> on some servers you really have to screen, otherwise the redirect does not work.
According to the current description, this is a special character.
Comment by: kumar on
example http://mywebsite.com/abc.php to http://mywebsite.com/abc/
Comment by: Ignacio on
Comment by: shambler81 on
before
(^|&).....($|&)
after
(?:^|&).....(?:$|&)
this will not affect the logic in any way, but it will be convenient to count the groups
Comment by: Jesse G Donat on
Comment by: Angelo on
You should write instead: "Check this goddamn awesome tool of mine, bro. Any comments are welcome below, ..."
Comment by: leandev on
Definetelly saved me alot of time
Comment by: Mar on
Comment by: Rick Altman on
I need a tiny bit of hand-holding to get started...
Comment by: Dave Reeder on
The fact it can do complex urls with query strings etc. it's very impressive.
Comment by: olivia jaacob on
Comment by: Nikiforov on
This is what I pasted to first field (old-new url):
https://www.domain.lt/old-category/subcategory/product-good-1-2 https://www.domain.lt/new-category/subcategory/product-good-1-2
This is what I get after I push “Submit Query” button:
# 301 --- https://www.domain.lt/old-category/subcategory/product-good-1-2 => https://www.domain.lt/new-category/subcategory/product-good-1-2
RewriteRule ^old\-category/subcategory/product\-good\-1\-2$ /new\-category/subcategory/product\-good\-1\-2? [L,R=301]
In the line „RewriteRule“ I see 8 characters „\“. Why? In my url no free space. You can test with my example and will get the same result. Of course I tried both variants and al lis working perfest, but questions is still. ????
Comment by: Flo on
@Nikiforov the backslash is to mark the special character in the string, it belongs there.