Multiple 301 Redirect For Multiple Pages or URL - .htaccess

I am redesigning my store and so the old structure has been changed with the new structure. So trying to redirect all old products with the new.
I have more than 20-25 products and if I write redirect rule for every product than I have to write in this way
Redirect 301 /store/products/somename/ http://store.domain.com/nicecar
Redirect 301 /store/products/blabla/ http://store.domain.com/newpros
Redirect 301 /store/products/cubacuba/ http://store.domain.com/illollo
Which will become to long and may slow down the site. So is there anyway to optimize this redirect rule?
Thanks a lot

It is better to use RewriteMap for your requirement. Here is an example how to use it:
Add following line to your httpd.conf file:
RewriteMap prodMap txt://path/to/prodMap.txt
Create a text file as /path/to/prodMap.txt like this:
somename nicecar
blabla newpros
Add these line in your .htaccess file under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^store/products/([^/]+)/?$ http://store.domain.com/${prodMap:$1} [L,R=301,NC]

If you have access to the vhost or server config, you can setup a rewrite map, though in reality, it's probably marginally faster than just having a ton of redirects. The redirects that you have in your htaccess file are cached, so as long as the htaccess file isn't modified, the directives don't need to be re-read.
There's a very detailed tutorial for how to use RewriteMap and the many various mappings you can use with it.

Related

htaccess rewrite rule assistance

I need to create a rewrite rule for an htaccess file which will rewrite URL's such as:
http://archive.citylaw.org/bsa/2014/03.24.14/300-13-A.pdf
to
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/2014/03.24.14/300-13-A.pdf
The rule needs to match any link such as:
http://archive.citylaw.org/bsa/<file-path>
which is a link to a PDF file to the new location
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/<file-path>
Something like:
RewriteRule ^(bsa\/.*) /wp-content/uploads/sites/24/$1 [R]
The [R] tell apache to send a redirect, so the browser will update its navbar, usually this avoid browser not dealing well with non html files on rewrite.
Updated after Prix comments, I did forgot about .htaccess specificity
Second edit

URL rewrites issues

We are having a problem with URL rewrites on an apache server using .htaccess.
Goal: to have the following URL stripped of its category & subcategory while leaving the generic redirect in place.
Test 1:
Redirect 301 /category/subcategory/product http://www.site.com/product
Redirect works perfectly. A single redirect to the desired page.
Test 2:
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
Redirect on its own works perfectly for all URLs desired.
The problem is when we have both URLs in a clean .htaccess file, and the redirects are in the proper order (specific first, then general), the general redirect is being used.
Test 3:
Redirect 301 /category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
When we visit www.site.com/category/subcategory/product, the result is www.site.com/category/subcategory/product, That is not the desired result. Instead, we want the URL to be www.site.com/category/subcategory/product,
We have even tried modified the Redirect to:
Redirect 301 /category/subcategory/product http://www.site.com/product [L]
It made no difference.
Please help!
EDIT: Added 3/25/2014
What we are trying to do is provide specific redirects for a group of known products from their old product page to the new product page. We are also trying to add a "catch all" redirect for the remaining unknown products to the category page.
Here is an actual example redirect which works:
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
If the above redirect is added to the .htaccess file, it works perfectly on its own.
Here is a second example redirect which works:
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
The problem is if we have both of the rules together in .htaccess, in the same order as above, the second rule is always triggered. We try to access www.site.com/womens/western-dresses/stetson-cream-empire-waist-ls-western-dress and the result is www.site.com/womens/western-dresses instead of the desired result of www.site.com/stetson-cream-empire-waist-ls-western-dress
For clarity:
if we remove the .htaccess file, the URL 404s
if only the first rule is listed, it triggers perfectly
if only the second rule is listed, the second rule triggers perfectly
if both rules are listed, the second rule triggers.
We have deleted all redirects from the .htaccess file. The only redirects are the below two lines. The issue remains where the first redirect is ignored. We have tried changing the start of the first redirect to ^/womens and ^womens but that change had no effect.
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
Your post is a little confusing, so I may be misunderstanding what you are trying to do.
If memory serves, you should not include a leading slash in your pattern when using these directives in a .htaccess file. That usage is reserved for httpd.conf. When these directives are used in a .htaccess file, the leading path components have already been stripped by mod_access. I am guessing this is the cause of your troubles.
For example, this should work (not tested):
Redirect 301 ^category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^category/subcategory/.* http://www.site.com/category/subcategory
As an aside, [L] is mod_rewrite lingo. "Redirect" and "RedirectMatch" are part of mod_access.
EDIT 3/25:
Redirect and RedirectMatch can be fussy when used in .htaccess files, particularly when dealing with non-existent folders and mixed directives. Can I suggest you move directly to mod_rewrite? While it has a steep learning curve, you will never go back once you get the hang of it.
# Assuming you are in a .htaccess under DocumentRoot:
RewriteEngine On
RewriteRule ^category/subcategory/product1\.html$ /product1.html [R=301,L]
RewriteRule ^category/subcategory/product2\.html$ /product2.html [R=301,L]
RewriteRule ^category/subcategory/.* /category/subcategory [R=301,L]
As an aside, this looks like a good candidate for RewriteMap, although you will need to declare the map in your httpd.conf.

.htaccess redirect subdomain to root domain

Maybe I just don't know how to ask the question to find the answer but what I want to do is an .htaccess redirect from an old URl, shop.nilandsplace.com/camp to nilandsplace.com/store/camping. Not to make it more difficult, but I would like to know what it is that I am doing so I can learn this
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop.nilandsplace.com$
RewriteRule ^camp(.*)$ http://nilandsplace.com/store/camping$1 [R=301,L]
The top line turns on the rewrite engine. We need to do this before rewriting any URLs.
The second line checks that we're visiting the shop.nilandsplace.com domain.
The third line redirects any requests from /camp to http://nilandsplace.com/store/camping. The bit in brackets at the end of the third line tells Apache to make this a 301 redirect (so search engines know the page has moved), and to do the redirect straight away, bypassing any upcoming rewrite rules for this request.
The code will also redirect subdirectories of camp (anything after camp), for example shop.nilandsplace.com/camp/tent-poles > nilandsplace.com/store/camping/tent-poles.
You can learn more about the mod_rewrite module (which powers the rewrite engine) on the Apache Docs.

redirect ALL urls from old site to index.php on new one with .htaccess?

A couple questions, simply:
Is the .htaccess file generally stored in the public_html directory? I think so, no?
If I do not find such file, can I simply create it and upload it with FTP?
3 (most importantly). What is the code I need to redirect ALL URLS to ONE new URL, namely, http://www.newsite.com
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Thanks
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Correct, it is the only thing you need. It will redirect anything starting with / to the appropriate place in http://www.newsite.com/. Example:
You go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/some/path/to/file.php.
If you want everything to go simply to the document root of the new site, you can use a RedirectMatch instead:
RedirectMatch 301 .* http://www.newsite.com/
So if you go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/
Not all apache installations come with mod_rewrite installed, if it's not installed you'll get a 500 server error if you attempt to use the rewrite engine. However, mod_alias is usually always installed.
Question 1: Whether you can just upload .htaccess to public_html depends on the web host, but that will likely work.
Question 2: I would set up the redirect like this:
RewriteEngine On
RewriteRule ^(.*) http://www.newsite.com/ [R=301,L]

Can one include separate files in htaccess for a list of redirects?

My main htaccess file does a bunch of things for my site to function correctly.
I have added redirects for pages that have moved. I don't have root access to the server and using .htaccess is my only option.
Is it possible to include separate files for the redirects in the .htaccess file so I can keep them separate and write programatically to the additional files that hold my redirects?
Basically I want to reference separate files from my .htaccess to manage rules dynamically and also neaten up one long .htaccess file with a few smaller files.
I also want to add redirect rules on the fly as things change on the site within my application.
You can use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html
Let's say your map file looks like this and is called moved.map:-
/about profile
/page/that/has/moved new/location
You .htaccess would need something like this:-
RewriteMap moved txt:moved.map
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond ${moved:%1|NOT_PRESENT} !NOT_PRESENT [NC]
RewriteRule .? ${moved:%1} [NC,R=301]
This will redirect with a 301 status code http://your.domain.com/about to http://your.domain.com/profile and redirect http://your.domain.com/page/that/has/moved to http://your.domain.com/new/location
You can then programmatically create moved.map.
I hope that helps.
If you are using .htaccess files then don't bother with RewriteMap -- it only applies if you have root access to the server or vhost config, which is never the case when you purchase a shared service offering.
If you are constrained to use .htaccess files then you have two options:
The first is to do what some packages do and that is to get your application to rewrite the .htaccess file based on a rewrite map that you maintain within in it. The best way to do this is to have "bookends" in your .htaccess file e.g.
##++AUTOMATIC rewrite rules
<rules inserted by your app>
##--AUTOMATIC rewrite rules
And when an update occurs have your app read in the .htaccess, swap out the section between ##(++|--)AUTOMATIC rewrite rules, write it back to a temp file, then move the temp file to .htaccess (this makes the rewrtie-back atomic on *nix OSs).
The second which might work if you know some regexp regular pattern which covers the rewrites (this is often the case) then use a rule to map them to a redirector script which looks up the new target and itself issues a:
$server = $_SERVER['HTTP_HOST'];
header( "Location: http://$server/$newTarget?$parameters", TRUE, 301 );
Note the 301 redirect -- this means that client browsers should cache this and remember this in future.

Resources