htaccess 301 from old site to new site - .htaccess

I have a client domain with thousands of pages that are moving to a new domain. The naming convention of the .html has changed, and I know htaccess can handle this somehow.
Here's an example:
old site:
http://oldsite.com/state/cityname-index.html
new site:
http://newsite.com/state/computer-support-cityname-index.html
This is beyond my understanding at the moment. I'd appreciate a little help. Thanks!

If you want to redirect the old website pages to the new one, you can use rewriting like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com$ [NC]
RewriteRule ^([^/]+)/(.+)-index\.html$ http://newsite.com/$1/computer-support-$2-index.html [QSA,R=301]
The first line activates the rewriting in the htaccess.
the second one checks if the current request is for the old website.
if yes, the third line is activated, wich contains a regular expression and a new link to redirect to if that regular expression if matched, the [QSA,R=301] at the end are flags that will affect the rewrite rule.
This rule I wrote to you captures the state (first parentheses) and the cityname (second parentheses) and then it redirects to the new website replacing the $n (where n is the number of the parenthese from before) with the captured content.
The QSA flag (Query String Append) will add any parameter from the old site request to the new generated request.
The R=301 flag will generate a browser Redirection with code 301 (permanant redirect).
For more information about mod_rewrite see http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

Changing Domain: 301 Redirect for multi language E-Commerce site

I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)
I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.
However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions.
(Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)
redirect function for products
redirect function for categories
redirect function for tags
individual redirects for the rest
There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.
Examples:
oldomain.com/product/any-product-ABC
redirect to
newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)
Then same redirect for languages
oldomain.com/es/product/any-product-ABC
redirect to
newomain.com/es/product/any-product-ABC
How do I write the above redirects?
I say htaccess but maybe there is a server side way that is better
If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.
Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.
For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:
RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0
This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]
Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.
Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).
You should first test with 302 (temporary) redirects to avoid potential caching issues.
so I thought it better to break into into chunks/functions.
That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.
And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.

htaccess How to redirect old dynamic url to the new one

My web uses links which are dynamic set by code in htaccess (bellow):
RewriteRule ^(.*),(.*),([a-z0-9-_.]+),([a-z0-9-_.]+),([a-z0-9-_.]+)$ $4.php?n=$1&z=$2&t=$3&v=$5 [L,NC,NS,NE]
In effect links looks like this (example):
www.mypage.com/$1,$2,$3,$4,$5
I want to redirect dynamic links in htaccess from old to new one which will have a structure like this (without $5 parameter):
www.mypage.com/$4/$1-$2/$3
Redirection is necessarily especially for redirect old links availble in search engines to new one.
Thanks for a help.
I don't have an Apache instance to hand to test against just now but off the top of my head something like this should work:
RewriteRule ^(.*),(.*),([a-z0-9-_.]+),([a-z0-9-_.]+),([a-z0-9-_.]+)$ $4/$1-$2/$3 [L,R=301]
RewriteRule ^([a-z0-9-_.]+)/(.*)-(.*)/([a-z0-9-_.]+)$ $1.php?n=$2&z=$3&t=$4 [L,NC,NS,NE]
The first rewrite redirects your old URLs (1,2,3,4,5) to the new ones (4/1-2/3) using a 301 to tell search engines to drop the old URLs in favour of the new ones.
The second rewrite takes the new format and maps it to your actual script.
Note how the 5th param is dropped when transforming old to new.

how to redirect URL containing query string and random numbers in .htaccess

When customers cancel a transaction on my site, they get redirected to the WooCommerce cart page with a query string containing randomly generated numbers at the end.
Example
https://www.example.com/cart/?woo-paypal-cancel=true&token=EC-5474842406066680S
(I need this redirect due to a plugin conflict between WP Rocket cache with CDN activated and WooCommerce. Long story.)
I'm wondering what exactly I would put in my .htaccess file to get it to redirect to
https://www.example.com/cart/
I've tried a number of variations I found on multiple pages here on Stackpath, but it wasn't redirecting. Obviously I'm missing something so I'm turning to the gurus.
Would be very grateful for your help.
To redirect /cart/?woo-paypal-cancel=true&token=<anything> to /cart/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^woo-paypal-cancel=true&token=
RewriteRule ^cart/$ /cart/? [R,L]
The ? on the end of the RewriteRule substitution strips the query string from the request.
This is a temporary (302) redirect.

how to rewrite a custom url in joomla using htaccess

I am designing a News Website using joomla 2.5
I want rewrite this url:
http://domain.com/categoryname/?format=feed&type=rss
to:
http://domain.com/rss/categoryname
Note: I'm using mode_rewrite .htaccess for joomla.
please help me quickly.
thanks to every body in this site.
Apache's mod_rewrite allows you to transform a url to a different url utilizing regex patterns.
The pattern applies to the path and allows you to do your in your example write a regex pattern like /rss/(.+) which will match anything beginning with /rss/ and has at least one character after. The parenthesis are called a capturing group and you can reference that in the second parameter in the RewriteRule directive.
The second part /$1/?format=feed&type=rss, references the first captured group in the pattern and places it in the new url.
Finally you want to signify that it is the last rule to be processed with an [L] flag.
This gives you a rule of:
RewriteEngine On
RewriteRule /rss/(.+) /$1/?format=feed&type=rss [L]
If you intend to pass query strings to this new url, you will need to add an additional flag QSA which will result in [L,QSA] in place of [L].

.htaccess 301 redirect of a dynamic url with defined first part and random last part

I'm new to this, so i searched and could also do alot alone. But now there's a point I need your help. It's about a e-commerce page.
I want to redirect the old URLs from our brand pages to the new one.
The old brand URLs looks like:
http://www.domain.com/index.php?shop_q=empty&cid=0&man=32&pf=0.00&pt=10000.00&p=shop&action=showproducts&list=date_asc&limit=10
the only thing i need out of this long url is man=32, what stands for the manufacturer (brand) id.
the new URL looks like:
http://domain.com/brand/8_brand-name
So I want that the rule to redirect every url with man=32 in it to the new brand URL, even if there are more characters in the URL.
Thanks for your help!
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)man=32($|&)
RewriteRule ^ /brand/8_brand-name? [L,R=301]
The condition's regex pattern, (^|&)man=32($|&), matches a specific query string parameter, and will match it no matter where in the query string it is.

Resources