Rewrite page url as subdomain in apache htaccess - wordpress

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.

Related

Redirect all pages from one domain to current domain

I have multiple subdomains, for example:
uk.domain.com
us.domain.com
eu.domain.com
And I need to redirect all of these subdomains (and any pages that the user tries to access on those subdomains) to:
www.domain.com
Example:
uk.domain.com/page1 -> www.domain.com/page1
uk.domain.com/page2 -> www.domain.com/page2
us.domain.com/page1 -> www.domain.com/page1
us.domain.com/page2 -> www.domain.com/page2
eu.domain.com/page1 -> www.domain.com/page1
eu.domain.com/page2 -> www.domain.com/page2
...etc
Is there a rule that can apply to all URLs on each of the subdomains and redirect them to the main domain, with the page in the URL intact, as above?
I have tried example here, but that did not work
Solved. This works
RewriteCond %{HTTP_HOST} ^uk\.something\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?$ [NC]
RewriteRule ^(.*)$ https://www.somethingelse.com/$1 [R=301,L]
However it needed to be at the top of my .htaccess, since I had other rules that were interfering with it.

How to redirect http to specific URL using htaccess in WordPress

I am looking for assistance with the redirection rules.
I have url www.example.com.au and I want the following redirections
https://www.example.com.au =>https://www.example.com.au
http://www.example.com.au =>https://www.example.com.au/page-2
I found the following .htaccess rules but its not working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} www^example\.com.au [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%.
{HTTP_HOST}%www^example.com.au/page-2 [302 L]
can anyone please help me the correct rule.
I will be using the above rule for a WordPress site so do i need to paste the above rules at the bottom so that it doesn't overruled by other rewrite rule?
Thanks in advance.
For the first redirection it is impossible, it will create an infinite loop. You can not redirect an url on itself.
And for the second one ( http://www.example.com.au => https://www.example.com.au/page-2 ) you can do something like that
Redirect 301 / https://www.example.com.au/page-2
Pay attention to 301, it indicates that the redirection is permanent, I do not know if it is your goal.
But if your goals was to redirect http to https you can do something like that (put on the top of your .htaccess file):
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.example.com.au/$1 [R=301,L]
Or use a plugin like Really Simple SSL
Then you can define your rules to redirect one page to another before the rule above, like this:
Redirect 301 /old-page /new-page

Nginx: I cannot get a 301 redirect from the Query Paramaeter to the Path

I would like to redirect:
From:
http://www.fascinate.jp/index.php?option=com_virtuemart&page=shop.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en
To:
https://www.fascinate.jp/english/brand/devoa/
The following is the redirect settings for my current site:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)
RewriteRule ^index\.php$ https://www.fascinate.jp/english/brand/devoa/? [L,R=301]
How should I rewrite the Apache Settings so I can use it with Nginx?
The version of Nginx I am using is nginx/1.9.11.
The second rewrite condition translates directly to an if block. The actual rewrite translates to a return 301 in its simplest form. Like this:
if ($args ~ "(^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)") {
return 301 https://www.fascinate.jp/english/brand/devoa/;
}
nginx is not good at implementing multiple conditions. The RewriteCond %{SERVER_PORT} 80 will only cause problems if you serve both SSL and non-SSL from the same server block. If you can separate out non-SSL into its own server block, then the above if block can be applied only to the non-SSL URLs.
See this and this for more. And of course this.

removing just "?m=1" from url using rewrite rule

just few day before i had migrated my blogger blog to wordpress. Now i find crawn error with many url, at the end of many url the name and value is there (?m=1) which shown as a 404 error now i want to redirect all the url additing .htaccess file
example:
http://www.tipsviablogging.com/blogger-tricks/facebook-disqus-tab-in-blogger.html?m=1
musy redirect to
http://www.tipsviablogging.com/blogger-tricks/facebook-disqus-tab-in-blogger.html
any one is having expertise in url rewrite kindly help me...
I haven't got a test system handy, but something like this in your .htaccess should do the trick:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]
If memory serves, you need the ? at the end of the target in the RewriteRule to stop the original query string being appended.
The code assumes you haven't got any other parameters (eg it won't work if you have ?m=1&foo=bar).
I want to add a solution on NginX:
Use below code in "location /" Of VirtualHost config
if ($query_string ~ "^m=1$"){
rewrite ^(.*)$ /$1? redirect;
}

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