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
Related
I have a lot of page URLs like domain.com/page I want a rewrite rule that will change all my pages URLs as page.domain.com meaning whatever will come after the domain just rewrite it as a subdomain
example urls
expertpro.cloud/hot-to-write-blog to hot-to-write-blog.expertpro.cloud
expertpro.cloud/game to game.expertpro.cloud
expertpro.cloud/nibm-full-form to nibm-full-form.expertpro.cloud
expertpro.cloud/choclate to choclate.expertpro.cloud
expertpro.cloud/harmony-in-life to harmony-in-life.expertpro.cloud
expertpro.cloud/paki-cold-places to paki-cold-places.expertpro.cloud
expertpro.cloud/you-are-one to you-are-one.expertpro.cloud
I already have some code for Nginx
The empty location = / block is necessary so that you don't redirect
http://example.com/ to http://.example.com/.
//replacing domain name in rewrite rule
location = / {
# don't redirect $server_name/
}
location / {
rewrite ^\/([^\/]*).*$ https://$1.$server_name/ redirect;
OK, all you need is an internal rewrite, as it looks:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
You obviously need the rewriting module to be loaded into your http server.
That would be a variant which additionally redirects direct requests to the internal URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?([^/]+)(/.*)?$ https://$1.example.com$2 [R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
Here it makes sense to start out with a R=302 temporary redirection and only to change that into a R=301 permanent redirection once everything works as expected.
In general you should try to implement such rules in the actual http server's host configuration. If you have no access to that you can instead use a distributed configuration file (".htaccess"), but those come with disadvantages. You need to enable the interpretation of such files in that case.
How to rewrite this Apache rule on Nginx?
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} ^.*(pdf|epub)$
RewriteRule ^(.*)$ /wp-content/themes/divi-child/download.php?file=$1 [L]
</IfModule>
The Apache mod_rewrite rule you specified redirects requests for files ending with the suffix pdf or epub to /wp-content/themes/divi-child/download.php?file=, with the filename appended to the end of the URI.
Using nginx, this could be accomplished with the following location block within your virtual host configuration:
location ~* \.(pdf|epub)$ {
rewrite ^/(.*(?:pdf|epub))$ /wp-content/themes/divi-child/download.php?file=$1;
}
This Stack Overflow question offers a bit of insight into how the pattern matching works for file extensions.
For more information, check out the nginx documentation and blog posts.
Been looking around the web to add a production rule in the .htaccess file. I have a wordpress website; one for production and the other, staging.
When a file is uploaded, it goes to AWS (s3). I need to prevent this behaviour for staging.
The code that sets the asset path is, in the .htaccess file:
RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
I cant seem to find an "if statement" or some sort of condition to use. Honestly, I think this is not possible. Is it?
I only need to run that code for production and not staging. Staging url is different from production.
Updated
Whenever I use below, my website crashes:
<If "-z req('Host') == 'www.<PRODUCTION>.com/'">
RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
</If>
If directive works on Apache 2.4 and newer versions. On lower versions you can use RewriteCond directive to conditionally rewrite urls.
You can use something like this
RewriteEngine on
#if host == "www.production.com"
RewriteCond %{HTTP_HOST} ^www.production.com$ [NC]
# execute the rule
RewriteRule ^wp-content/uploads/(.*)$ https: //s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
We have a server where nginx is installed, we have also configured PHP As FastCGI on the server. Everything is working fine except rewrite rule. Our requirement is to mask an URL
for eg:- if someone search in our website the URL which comes will be like http://example.com/search.php?searchword=$1 ($1=searched word) . We need to display URL for our customers as http://example.com/$1.html.
We have set rewrite rule as rewrite ^/(([a-zA-Z_0-9]|-)+/?)$ /search.php?searchword=$1 break;
The URL is getting redirected however we get a file not found error each time. How can we mask the URL just as we do in Apache. Any help would be greatly appreciated
Equivalent Apache htaccess rules which we used are as follows
RewriteCond %{REQUEST_URI} !index\.html$ [NC]
RewriteRule ^([a-zA-Z0-9-/]+).html$ search.php?searchword=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ search.php?searchword=$1 [L]
It was working fine with Apache
You should put this rewrite code to location / . I've tested it on my server.
location / {
rewrite ^/([a-zA-Z0-9/-]+).html/?$ /search.php?searchword=$1 last;
}
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%