301 Redirect Rules for a Blog Migration - .htaccess

I want to implement 3 redirect rules for a blog migration where each page will be shifted to a sub-folder structure. It currently sits at a sub-domain.
I can't really screw this one up and want to make sure I nail the correct generic rules for the 3 type of existing URLs:
Homepage
Current:
https://blog.lang.example.com
Goal:
https://www.example.com/lang-country/news/
Category
Current:
https://blog.lang.example.com/category/category-name
Goal:
https://www.example.com/lang-country/news/category/category-name
Post
Current:
https://blog.lang.example.com/yyy/mm/dd/article-name
Goal:
https://www.example.com/lang-country/news/yyy/mm/dd/article-name
Is this something you can help?

Unless you have other URLs that you don't want to be redirected then you can do something like what you require with a single redirect near the top of the .htaccess file in the subdomain's document root.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.([^.]+)\.example\.com [NC]
RewriteRule (.*) https://www.example.com/%1-country/news/$1 [R,L]
Where %1 is a backreference to the lang subdomain in the requested host.
However, you still have the problem of where country should come from in the target URL, since this is not present in the source URL. You will either need to default this to something or implement some kind of lookup based on the language. This would need server config access (to configure a RewriteMap) if you wanted to do this in .htaccess. Or, you implement this redirect entirely in your server-side script (eg. PHP).
Note that this is currently a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect once you have tested that everything is working OK (if a 301 is required). 301 redirects are cached hard by the browser so can make testing problematic.

Related

HTACCESS 301 redirect keep sending to the wrong page

I am trying to redirect an old page from a website I have redesigned, to the new one, but it's not working.
Here's my 2 lines of code in the .htaccess file regarding that domain:
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]
If go on the solutionsgtr.ca/deaneco/contact URL, it gives me the following page:
http://solutionsgtr.ca/fr/deaneco/accueil.html/contact
The first rule works though (deaneco/ to solutionsgtr.ca/fr/deaneco/accueil.html).
I feel like both lines are being mixed together and are giving me the wrong page, that doesn't exist so I get a 404 error.
There are a couple of issues here:
The Redirect directive (part of mod_alias) is prefix-matching and everything after the match is appended on the end of the target URL. This explains the redirect you are seeing.
The RewriteRule (mod_rewrite) pattern ^/deaneco/contact will never match in a .htaccess context since the URL-path that is matched does not start with a slash. So, this rule is not doing anything currently.
You should avoid mixing redirects from both modules since they execute independently and at different times during the request (mod_rewrite executes first, despite the apparent order of the directives).
Either use mod_alias, ordering the directives most specific first:
Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
NB: You will need to clear your browser cache, since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
OR, if you are already using mod_rewrite for other redirects/rewrites then consider using mod_rewrite instead (to avoid potential conflicts as mentioned above):
RewriteEngine On
RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]
The QSA flag is not required, since the query string is passed through to the substitution by default.
The order of the RewriteRule directives are not important in this instance, since they match just that specific URL.
If go on the solutionsgtr.ca/deaneco/contact URL
If you are redirecting to the same host then you don't need to explicitly include the scheme + hostname in the target URL, since this will default.

Redirect one URL to another on same domain with htaccess

I am currently building a new website. The old website has different url's than the new one. Now i want to redirect, using htaccess.
Before going live, i want to test my rules locally.
The old website uses url's of this format:
www.domain.de/?content=whatevercontent
The new website uses url's of this format:
www.domain.de/index.php?content=differentcontentname
Which are rewritten (in the htaccess file, using several RewriteRules) to this format:
www.domain.de/nicecontentname
I tried Redirects like this:
Redirect http://domainfolder/?content=whatevercontent http://10.3.10.69/domainfolder/nicecontentname
This does not work.
After going live it should work like this:
Redirect http://www.domain.de/?content=whatevercontent http://www.domain.de/index.php?content=differentcontentname
..and then be rewritten to the nice url.
My Redirect-Rules just won't apply, i tried it in all combinations i could think of, with or without http, with or without the containing folder, using the already rewritten url or the actual one, etc..
Any ideas on this issue?
You can not use Redirect directive to manipulte querystrings. Here is a mod-rewrite example that works for QueryStrings :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/\?content=this
RewriteRule ^$ /index.php?content=that [L,R]
This will temporary redirect /?content=this to /index.php?content=that
To make the redirect permanent (browser cacheable) change R to R=301.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

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.

Resources