301 redirect adding unnecessary query string to new url - .htaccess

I'm trying to set up 301 redirects from a subdomain to the main domain, but I'm running into issues when the old urls have query strings.
The new site is running PHP as aFastCGI process. I tried using the solution listed here: https://expressionengine.stackexchange.com/questions/5188/301-redirects-appending-query-string
and it works for most urls, but not when the old urls have query strings.
For example,
RewriteRule ^old-blog/title/?$ http://www.example.com/new-blog/title/ [L,R=301]
works to redirect old.example.com/old-blog/title to www.example.com/new-blog/title but I'm
having trouble with pages that include query strings. I tried using this:
RewriteRule ^Default\.aspx?/?$ http://www.example.com/new-blog/ [L,R=301]
but a url like old.example.com/default.aspx?title redirects to www.example.com/new-blog/?title instead of just www.example.com/new-blog/
Can I stop the query strings from being appended to the new urls?
Also is there a way to just do a blanket redirect of every link that ISN'T already redirected using the first method above to www.example.com/new-blog/ ?
Not sure if it matters but the new site is running ExpressionEngine and using htaccess to remove index.php from the urls, but the htaccess rules above are on the old subdomain.

Related

How can we implement 301 redirects for the below scenario on .htaccess file

Old url: www.vanguardinvestments.com.au/businessline/product.html#/fundDetail/wholesale/portId=8162
New URL: www.vanguard.com.au/businessline/products/en/detail/8162/Overview
We have 60 old urls of same pattern but only the portId will be changing in every url. Could someone please suggest how can we implement a generic redirect which will redirects all 60 old urls to new url (taking the portId value from old url) for this type of scenario.
You can use RewriteRule in your htaccess
RewriteEngine on
RewriteRule ^businessline/products/en/detail/([0-9]+)/Overview www.vanguardinvestments.com.au/businessline/product.html#/fundDetail/wholesale/portId=$1 [NE,L,NC,R=301]
If the portid perameter contains alphanumeric characters then change the regex pattern to ([0-9a-zA-Z_]+) .
This redirect your new URL to the old one. If you are looking for a way to redirect your old URLs to the new one with fregment # then you will need to use a client side redirect like JavaScript as # redirection can not be implemented by using RewriteRule or any other server side redirect.

301 Redirect Rules for a Blog Migration

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.

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.

forcibly redirect to correct folder

I am new in htaccess.
I updated some SEO pages in my live site after one day some Url changes came so i changed the url again. but google already indexed it. So i want if some one found old url it will redirect to new url But in case of SEO pages only not for other pages.It means it wont affect to any other place.and there are not one page(it is 40-50 pages) can anybody give answer through htaccess or cakephp.
Old Url-
www.testenergy.com/test-energy-reviews
new url-
www.testenergy.com/s/test-energy-reviews
And there are also four senario-
www.testenergy.com/test-energy-reviews
www.testenergy.com/Test-Energy-Reviews
www.testenergy.com/s/test-energy-reviews
www.testenergy.com/s/Test-Energy-Reviews
All these four links will redirect to www.testenergy.com/s/test-energy-reviews Url only
Assuming you have mod_rewrite rules somewhere, you probably want to stick to mod_rewrite. You'll need to add these to the htaccess file in your document root, preferably above any other rules that are there:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]
RewriteRule ^/?s/Test-Energy-Reviews$ /s/test-energy-reviews [L,R=301]
The NC flag ignores case, so it covers both /test-energy-reviews and /Test-Energy-Reviews. The second rule takes care of /s/Test-Energy-Reviews
I'm not sure why /s/test-energy-reviews (3rd one) is one of your scenarios, since it is exactly what you want to redirect to.
Try This ..!!
Router::redirect('/test-energy-reviews', 'http://www.testenergy.energy/s/test-energy-reviews');
write this line in Controller.
/********* Redirect Url fo small letter if some one type in uppercase in url bar****/
preg_match( '/[A-Z]+/',$this->params->url, $upper_case_found );
if(count($upper_case_found)) {
// Now redirect to lower case version of url
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . ROOTPATH.strtolower($this->params->url) );die();
}
/**** End Code******/
OR in htaccess write following code:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]

.htaccess redirects - updated site (301 appropriate?)

I've run into a problem updating my site, the google search results show up links to the old page which are 404's now, some of them even containing deprecated content.
My question is about the use of 301's. The old page had deeply nested pages like the example below:
www.site.com/category/subjects/subject_b.html
It shows up in google with a very specific description of 'subject_b', which is not optimal for my purposes.
The new layout I've been working on means that content that was once under '/category/subjects/subject_b' is now found in a single page (www.site.com/subjects.html), along with the hypothetical subject_a & subject_c.
Would I be wrong redirecting the old pages like this?
redirect http://www.site.com/category/subjects/subject_a.html http://www.site.com/subjects.html
redirect http://www.site.com/category/subjects/subject_b.html http://www.site.com/subjects.html
redirect http://www.site.com/category/subjects/subject_c.html http://www.site.com/subjects.html
Also, how would I deal with pages that have google descriptions with content which is not on the new equavilient page?
I'd be happy if anyone could shed some light on this for me, or point me in a right direction as to where I can read more about it!
Would I be wrong redirecting the old pages like this?
You'd remove the http://www.site.com part. This should suffice:
Redirect 301 /category/subjects/subject_a.html /subjects.html
Redirect 301 /category/subjects/subject_b.html /subjects.html
Redirect 301 /category/subjects/subject_c.html /subjects.html
Or even:
RedirectMatch 301 ^/category/subjects/subject(.*) /subjects.html
to cover for all 3.
If you have multiple domains all pointing to the same document root and you actually need to match the path for a specific domain, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com$ [NC]
RewriteRule ^/?category/subjects/subject(.*) /subjects.html [L,R=301]

Resources