In IIS 7.5, using global.asax, I intend to redirect as follows.
The scenario is this, 1. http + domainname should be redirected to https + www + domainname.
http + www + domainname should be redirected to https + www + domainname.
Code below
string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();
string newURL = currentUrl;
try
{
Response.Status = "301 Moved Permanently";
newURL = newURL.Replace("http://domainname", "https://www.domainname");
newURL = newURL.Replace("http://www.domainname", "https://www.domainname");
Response.AddHeader("Location", newURL);
Response.End();
}
catch(Exception ex)
{
}
It gets into an infinite redirect loop and it doesn't load the site at all. Can someone please help?
Click here to view Site Binding screenshot
Looks like you need to check, after currentUrl is assigned, whether it is in the format you desire -- https + www + domainname . If it isn't then do your redirect code, but if it's already correct, skip the redirect stuff.
Please try with IIS rewrite module, that will be cleaner.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Related
Is it possible to rewrite or redirect an url to a user friendly url without changing the requested url in the browser ?
I want user to access the page :
example.com/dashboard/c7BIDZMJ96zh8jKgmTEcAugA22RDhGkH
Through url :
example.com/dashboard/nicedashboardname
I am using Nginx rewrite
location = /dashboard/nicedashboardname {
rewrite ^/dashboard/nicedashboardname?$ /dashboard/c7BIDZMJ96zh8jKgmTEcAugA22RDhGkH break;
}
I need this because the Id of the non-user-friendly page can change over time.
Thank you
No need to use rewrite hereā¦ You can use return:
location = /dashboard/nicedashboardname {
return 301 /dashboard/c7BIDZMJ96zh8jKgmTEcAugA22RDhGkH;
}
I am trying to setup bulk redirections in nginx(openresty) with map directive but am facing some issues with capturing regex/query string from the source url and forward to destination url. below is like my setup
nginx.conf is like
map_hash_bucket_size 512;
map_hash_max_size 32768;
map $uri $new_redirects {
default "none";
include /usr/local/openresty/nginx/conf/new_redirect.map;
}
Server Block
if ($new_redirects != "none") {
return 301 $scheme://$http_host$new_redirects;
}
new redirect map contains below redirects
~^/test/123/(\w+)(\w+).*$ /US/en/test/$1-$2;
Which is working good
but i am struggling a lot to get for the below strings
Part of url for Regex Capture
/Product.html?ProdNo=A5441&Brand=COMPANY
Captures in the new url
/US/en/product/COMPANY-A5441
2.Part of url for Regex Capture
/ProductDetail.do?&N5=SEARCH_CONCAT_PNO|BRAND_KEY&F=SPEC&N4=AV35016|COMPANY
Captures in the new url
/US/en/product/COMPANY-AV35016
Any help would be greatly appreciated, Cheers all!
I am implementing when user enters "m.co-oprating.com" it will be redirect to "www.co-oprating.com/mobile", but when I used the following code it doesn't work:
if (Request.Url.Host.StartsWith("m."))
{
UriBuilder builder = new UriBuilder(Request.Url);
builder.Host = "www." + Request.Url.Host;
Response.Clear();
Response.StatusCode = 301;
Response.StatusDescription = "Moved Permanently";
Response.AddHeader("Location", "www.co-oprating.com/mobile");
Response.End();
}
Was it because the request did not reach co-oprating.com at all, do I have to fix the DNS ?
I have to fix the DNS
Yes.
nslookup
> m.co-oprating.com
Server: google-public-dns-a.google.com
Address: 8.8.8.8
*** google-public-dns-a.google.com can't find m.co-oprating.com: Non-existent domain
I have a Magento v1.4.1.1 installation on Nginx web server. I'm trying to rewrite the following dynamic URL:
#5028 is the dynamic id passed in the URL
$baseUrl/design/index/index/design_id/5028
To:
$baseUrl/my/design/5028
I have used the Magento's "URL Rewrite Management" to rewrite static URLs, without a problem. But it seems I cannot use dynamic parameters.
I've also tried to do the following on Nginx configuration inside my server{} location
rewrite ^/my/designs/([0-9]+)$ /design/index/index/design_id/$1 last;
But it's not getting catch, I keep getting 404 errors if I try to access http://mysite.com/my/design/5028
Another rule right next to this one works perfectly
rewrite ^(/fb)/design/([0-9]+)$ $1/landing_no_contest.php?design_id=$2? last;
Thanks for any help.
Yes, Magento's "URL Rewrite Managment" does not support dynamic links, afaik.
You could programmatically add static rewrites per design_id to it, though:
$iStoreId = 1;
$sOptions = 'RP'; // 'RP' for a 301, or 'R' for a 302
$aDesignId = array(5026, 5027, 5028);
foreach ($aDesignId as $iDesignId) {
Mage::getModel('core/url_rewrite')
->setStoreId($iStoreId)
->setCategoryId(null)
->setProductId(null)
->setIdPath(str_replace('0.', '', str_replace(' ', '_', microtime())))
->setRequestPath('design/index/index/design_id/' . $iDesignId)
->setTargetPath('my/design/' . $iDesignId)
->setIsSystem(0)
->setOptions($sOptions)
->save();
}
When I check my page using those online host header analyzers, the page says 200 OK.
But when viewing in my browser, it redirects to another website (which is how I want it to be).
The code that I use is:
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", "http://www.example.com/article" + articleID);
context.Response.End();
I put this code in a HttpModule.
it is working because when you try and hit the url, it does the redirect.
It just doesn't seem to be returning the correct http header.
Is there something wrong?
Make sure the response buffer is completely clear before you add your headers:
context.Response.Clear();
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", "http://www.example.com/article" + articleID);
context.Response.End();
try:
context.Response.StatusCode = 301;
context.Response.StatusDescription = "Moved Permanently";
context.Response.RedirectLocation = "http://www.example.com/article" + articleID;
context.Response.End();
I use the above in a custom module and it does return a proper 301 HTTP response.
Your code is exactly correct. I've used exactly what you've got for years:
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location",URL);
context.Response.End();
Could you try using:
HttpContext.ApplicationInstance.CompleteRequest()
Instead of Response.End()?
When I use the HTTP logging in Web Development Helper, I see the 301 and the 200. So, yes, your code is correct.