I have in a site with many user subdomains ie user1.example.com or user2.example.com.
I have the following rewrite rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]
However this only works with same name folders. I want all subdomains to go to a single folder I have setup to parse the domain. How can I make all subdomains redirect to one folder?
Related
My website is located in a subfolder (gng2) of my public_html folder. I used the second part (##2) of the below .htaccess file to rewrite the url to add the gng2 subfolder to the URI. It works fine, the app loads when I enter my url to the browser.
Now I added the first part (##1)to redirect any requests where the url contains the subfolder as well. I want www.staging.gonativeguide.com/gng2/en to be redirected to www.staging.gonativeguide.com/en. However this does not work: the first url does not get redirected to the second. Checking the below code on htaccessTester, it says it is correct and it should redirect.
My websited is hosted by a shared hosting service and I think the web server is nginx. Any idea why the redirect does not work?
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} gng2/
RewriteRule gng2/(.*) www.staging.gonativeguide.com/$1 [R=301,L]
## 2. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
There are a number of issues with your current attempt. So I sketched an alternatate version which simplifies the rules and enhances robustness:
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteRule ^/?gng2/(.*)$ https://www.staging.gonativeguide.com/$1 [R=301]
## 2. redirect request that do not contain the "www" prefix in the host name.
RewriteCond %{HTTP_HOST} ^staging\.gonativeguide\.com$
RewriteRule ^ https://www.staging.gonativeguide.com%{REQUEST_URI} [R=301]
## 3. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteCond %{REQUEST_URI} !^/gng2/
RewriteRule ^ /gng2%{REQUEST_URI} [END]
You need to make absolutely sure that you are not looking at cached results when testing. So always test using a fresh anonymous browser window and use "deep reloads" (CTRL-F5 typically) instead of just reloading.
My website is located in a subfolder (gng2) of my public_html folder. The below code from the public_html/.htaccess file works fine in the sense that typing my domain loads the website properly from the subdirectory:
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
My problem is that www.example.com/gng2/whateverpage also works. I want to rewrite/reroute these urls to www.example.com/whateverpage so that the "gng2" subfolder does not show up for example in Google Analytics results.
How should I modify the above code to achieve this?
Thanks,
W.
You can use this redirect rule in gng2/.htaccess to remove /gng2/ from all URLs:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+gng2/(\S*) [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# other rules appear below this line
Please keep your htaccess file inside gng2 folder and try following rules in your htaccess rule file. Please make sure these rules are at top of your file(for applying http/HTTPS to URLs, in case you have more rules), also please do clear browser cache before testing your URLs.
RewriteEngine ON
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
##Place your RewriteRule here.....
My website (https://www.went.digital) have a wildcard subdomain (but sadly not a wildcard SSL), which redirects to a subfolder. That's mean that if you enter to demo.went.digital, you'll see what's in went.digital/subdomain. It doesn't really matter how does it work, but I'm saying it because the .htaccess file affecting the subdomains too.
I add into the .htaccess file code that automatically adds www if it's not a subdomain, and I add to it https:// but it doesn't add the https:// if you entered using www.
Here is my current code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
You'll have to add a separate rule for that:
RewriteCond %{HTTP_HOST} ^www\.went\.digital$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.went.digital/$1 [L,R=301]
I have this rule in my htaccess:
RewriteCond %{HTTP_HOST} !^(www|cdn)\.domain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.domain\.com$
RewriteRule ^(.*)$ user/%1/$1
right now http://username.domain.com/ works good, internally redirects to http://domain.com/user/username.
My goal is to redirect everything inside "user" folder, obtaining something like this http://username.domain.com/subfolder2/sybfolder3/ that redirects internally to http://domain.com/user/username/subfolder2/subfolder3/
I have 2 domains: main.com and addon.net
On my shared hosting account I create an addon-domain foraddon.net which automatically creates a folder in the main-domain's directory as well as a subdomain.
I want to change the accessability of the addon domain via the maindomain:
http://addon.main.com
http://main.com/addon.net/
Now both serve the index.html from addon.net
Both URLs should result in a "404 - not found" error.
What I have right now on main.com/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon.net/(.*)$
RewriteRule ^(.*)$ 404.html [L]
And in addon.net/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.main.com$ [OR]
RewriteRule ^(.*)$ http://www.main.com/ [R=301,L]
And now everything redirects to main.com:
http://main.com/addon.net/ redirects to http://www.main.com
http://addon.main.com redirects to http://www.main.com
http://addon.net redirects to http://www.main.com
My question: which rules should I add to which .htaccess-file in order to get the desired results:
addon.main.com redirecting to main.com/404.html
main.com/addon.net redirecting to main.com/404.html
addon.net serving addon.net/index.html
If you want to redirect access from anything except addon.net (and redirect the other requests to a 404 page), all you need to do is use these lines in the .htaccess file inside addon.net :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?addon\.net$ [NC]
RewriteRule - /404.html [L]