URL Re-Writing in ASP.Net - asp.net

I want to implemewnt URL-Rewriting in way that user can access website with username.domain.com
e.g.
www.abc.com/login.aspx
I should be able to access this like
www.username.abc.com/login.aspx
blogspot is also one of the example like http://username.blogspot.com/
Plz suggest me how can I accomplish this.
Thanks

Basically what you need to do is use a tool like the Managed Fusion URL Rewriter and Reverse Proxy, with the following rule.
RewriteCond {HOST} www\.(.*)\.abc\.com
RewriteRule ^/login.aspx$ /login.aspx?domain=%1
RewriteRule ^/login.aspx?domain=www\.(.*)\.abc\.com$ /login.aspx?user=$1
So it will come through to your internal application like
URL: www.nick.abc.com/login.aspx
Internal URL: www.abc.com/login.aspx?user=nick
The thing you have to solve which you didn't address is how are you going to get the users name and how are you going to handle them internally.
But really you don't need a URL Rewriter. You just forward all DNS traffic to the same IP address, and then you handle the domain with in your application instead of controlling it through the DNS.

It is possible with ISAPI Rewrite installed on the server
You have to put this in the httpd.ini file of the website
# Convert http://example.com to http://www.example.com/
RewriteCond Host: ^example.com
RewriteRule (.*) http\://www\.example.com$1 [I,RP]
# Assuming we have limited number of shared folders.
# We will execute them accordingly regardless of the subdomain.
# Example: http://sub1.example.com/img/logo.jpg -> /img/logo.jpg
# Example: http://www.example.com/img/logo.jpg -> /img/logo.jpg
RewriteRule (/css/.*) $1 [I,O,L]
RewriteRule (/js/.*) $1 [I,O,L]
RewriteRule (/img/.*) $1 [I,O,L]
#Redirect all other subdirectories not matching
#to the list above as subdomains
#example: www.example.com\sub1 -> sub1.example.com
RewriteCond Host: www\.example\.com
RewriteRule /(\w*)/(.*) http\://$1\.example\.com$2 [I,RP]
# If the web site starts with www then point the file to the root folder
# If you specifically created a folder /www/ then you can comment out this section.
RewriteCond Host: (?:www\.)example.com
RewriteRule (.*) $1 [I,O,L]
# Any web site starts other than www will be re-mapped to /<subdomain>/
# Example: http://sub1.example.com/default.asp -> /sub1/default.asp
# Note: if the folder does not exists, then the user will get a 404 error automatically.
RewriteCond Host: (.*)\.example.com
RewriteRule (.*) /$1$2 [I,O,L]
#Fix missing slash char on folders
#This has to be at the end because if invalid dir exists,
#we should show 404 first
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,RP]
The vital part is this one:
# Any web site starts other than www will be re-mapped to /<subdomain>/
# Example: http://sub1.example.com/default.asp -> /sub1/default.asp
# Note: if the folder does not exists, then the user will get a 404 error automatically.
RewriteCond Host: (.*)\.example.com
RewriteRule (.*) /$1$2 [I,O,L]
This is the best way I could find. If you don't have access to the server sittings and don't have ISAPI installed then this is not for you.
Here's the link to the article
http://www.seoconsultants.com/windows/isapi/subdomains/

Try this in web config under system.web
<system.web>
<urlMappings enabled="true">
<add url="~/myaccount" mappedUrl="myaccount.aspx"/>
</urlMappings>
in code behind file write
Response.redirect("~/myaccount")`
This works 100%

Related

Redirect domain.org to www.domain.org

I have the following doubt.
On a Debian server with apache I have a wordpress published with the domain "mydomain.org".
I want to be able to do the following:
When in the browser I write http://mydomain.tld it redirects me to http://www.mydomain.tld, that is, redirect a non-www request to www request.
When accessing http://mydomain.tld, what appears on the screen is the "Locked Page" message from the firewall in front of us, so I understand that the problem itself is on the side of the firewall in the configuration itself of the WAF.
Can it be correct?
Thank you very much in advance
1) Create an .htaccess file in your root directory where wordpress exist. If you already have an .htaccess file in the root directory, you can modify it.
2) Copy and paste the following lines of text into the .htaccess file: paste any one code as per your requirements.
#Remove www from any URLs that have them: Paste any one code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Add www to any URLs that do not have them: paste any one code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3) Replace example.com in the final line of text with your own domain name.
4) Save the .htaccess file.
5) To test the configuration, use your web browser to visit http://www.example.com, where example.com represents your domain name. The browser should be redirected to http://example.com.

FOSRestBundle double leading slash in route

I'm trying to handle a wrongly coded leading slash route in an Android application. It is trying to reach our system using:
//api/1.0/store/products/video/USD.json
but should normaly be
/api/1.0/store/products/video/USD.json
so this is causing a route not found exception in our app.
I cannot change the android application! I must find a way to make the wrong route work!
What i've tried:
Splitting the controller in two, one with the standard "/api" prefix and one with a prefix of "//api", didn't work, i think FOSRestBundle is fixing that for me live, so all my routes are still only 1 leading slash
Using a rewrite rule in .htaccess (See below) to rewrite the rule before i get problems, this would be the best strategy as it would keep my app and integration tests clean
Attempt using HTACCESS
RewriteRule /api/(.*) api/$1 [L]
This rule is supposed to work if i test it on "http://martinmelin.se/rewrite-rule-tester/" but in my htaccess it doesn't. Here's the content of my .htaccess.
Can anyone help me figure out a solution?
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite double leading slash routes to single leading slash routes
RewriteRule /api/(.*) api/$1 [L]
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
Okay, I tried, and failed to match the requested uri, so I will suggest you different approach, if you find it suitable. How about if we just replace all double leading slashes with single ones?
What I mean is something like that:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
The following tests on my project were successful:
/admin//orders => /admin/orders
//admin//orders//5 => /admin/orders/5
And lastly, I pasted yours as well:
//api/1.0/store/products/video/USD.json
gave this:
No route found for "GET /api/1.0/store/products/video/USD.json"
which is I believe what we are looking for. Hope you can use this solution as a temporary one until someone else provides a better one.

301 redirect loop in WordPress

Initially used a 301 to redirect www.example.com/ → www.example.com/wp
Unfortunately, I didn't read all the 'don't use 301 unless your 100% sure it's permanent' and now I need to revert back to the original domain.
At first, I tried to do a regular site url/wordpress url change in Setting/General in the admin dash. Saved over the old .htaccess on the root that had the original 301 redirect. Didn't work.
I moved everything to the root directory because I was getting a 'This webpage is a redirect loop' error page. Cleared cache on all browsers. Still getting the redirect loop error page.
Checked my url redirection here: http://www.digitalcoding.com/tools/url-redirect-check.html
I have two prompts, the first is going through fine, the second is a big fat X in red:
301 Moved Permanently: www.example.com/ → www.example.com301 Another Redirect Detected: www.example.com
.htaccess looks like this:
#Use PHP 5.4
AddType application/x-httpd-php54 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php54/lib/php.ini
</IfModule>
ErrorDocument 401 default
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any insight?
Redirect 301 / http://www.example.com/
You get an endless loop because there is nothing in that code to tell it not to redirect www.example.com to itself.
You are correct that you cannot use the Redirect directive, and this is the reason; It is unconditional, and will cause a loop in the scenario you describe.
In order to prevent the loop, you must find a way to tell the code not to redirect www.example.com to itself. This can be done by using mod_rewrite, and specifically, the RewriteCond directive in mod_rewrite, to test the requested hostname and act accordingly:
Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# if requested hostname is non-blank
RewriteCond %{HTTP_HOST} .
# and if requested hostname is NOT "www.example.com"
RewriteCond %{HTTP_HOST} !^www\.example\.com
# redirect to same object in correct domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The first directive, Options, may or may not be required on your server. If it is not required, it may in fact not be allowed. Comment it out of you have trouble.
The second directive, RewriteEngine, is required once (and only once) at the top of your mod_rewrite code.
The third directive, the first RewriteCond, is only required if you do not use a name-based virtual (shared) server. It prevents an infinite loop if the client does not send a "Host" header with its request. Since it is impossible to access a name-based virtual server without a "Host" header, this line is not required on a name-based virtual server. No harm will come from leaving it in, except that it takes a little time to process it.
Note that this code will also redirect "example.com" to "www.example.com", and so serves to canonicalize your main domain name as well, preventing ranking dilution from having duplicate content on two variations of the domain.

How to add www. to requesting URL on a Drupal site?

I'd like to redirect, for example:
http://example.com/ -> http//www.example.com/
http://example.com/node/1 -> http://www.example.com/node/1
https://example.com/cart -> https://www.example.com/cart
Thanks!
In the root directory of your Drupal site, check out the comments in the .htaccess file (in the # Various rewrite rules section. Specifically, look for the following lines:
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
... and uncomment those last two, replacing example.com with the appropriate domain name.

Map Domain Alias to Virtual Folder in IIS6

How could I go about mapping a domain alias, e.g. domainAlias.co.za, to a virtual folder under, e.g. mainDomain.co.za, so that all requests to domainAlias.co.za actually get served by mainDomain.co.za/domainAlias ?
A URL Rewriter like IIRF lets you do this.
The rules would be:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteRule ^/(.*)$ /%1/$1 [L]
In English, this rule says: if the host is NOT maindomain.co.za, but still ends in .co.za, then rewrite the URL so that it is prepended with /domainAlias/. With this rule, you get:
input output
----- ------
http://foo.co.za/a.php http://main.co.za/foo/a.php
http://foo.co.za/a.aspx?r=1 http://main.co.za/foo/a.aspx?r=1
You can also go one level further and make the rewrite conditional on the presence of the directory, something like this:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1 -d
RewriteRule ^/(.*)$ /%1/$1 [L]
This says: if the host is not maindomain.co.za, AND the directory c:\wwwroot\domainAlias exists, then rewrite to prepend ....
But in that case you might instead want to do the converse - test for lack of existence of the directory - and redirect to a 404:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1 !-d
RewriteRule ^/(.*)$ - [NF]
NF = 404
you can also do [F] which is a 503 (Forbidden).
IIRF works on IIS5, IIS6, or IIS7.
I haven't used it, but IIS has a URL Rewrite Module that can import Apache mod_rewrite rules. There is also a document that compares IIS URL Rewriting and ASP.NET routing. With some research, you should be able to get that working.
You can use routing.
System.Web.Routing

Resources