.htaccess - Rewrite https://www.sub.domain.com to https://sub.domain.com - .htaccess

I got a .htaccess issue, and I don't know why, because it should be working.
What I'm trying to do with this code is redirect https://www.sub.domain.com to https://sub.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://sub.domain.com/$1 [L,R=301]
When I test this code at http://htaccess.madewithlove.be/, it works succesfully.
(Request url: https://www.sub.domain.com, output url: https://sub.domain.com)
Also as mentioned in this question, I checked my DNS records , and there is an www.sub which points to the some domain as sub (both A's).
At this point I'm stuck, does anyone know what I did wrong?
Any help would be appreciated.

Create a file called .htaccess in the doc root (web / httpdocs / www folder) of https://www.sub.domain.com and put in the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://sub.domain.com/$1 [R=301,L]

Related

How to get /folder/subfolder to display as /subfolder/ using htaccess

I'm trying to do the following...
Original path that needs to be rewritten, renamed, or redirected:
http://www.example.com/_plugin/notifications/
to:
http://www.example.com/notifications/
via root htaccess file...Any help would be greatly appreciated!
=================================================================
This does not work:
RewriteEngine On
RewriteRule ^/_plugin/notifications$ /notifications [L]
This does not work either:
RewriteEngine On
RedirectMatch ^/_plugin/notifications$ /notifications/
This does not work either:
RewriteEngine On
RewriteRule ^notifications/(.*)$ /notifications$1 [R=301,NC,L]
=============================
EDIT
With the help of #Starkeen - I have a few follow up questions to this.
Is there a way to shorten the .htaccess file up though if lets say I
have multiple subfolders within the folder instead of writing that 1
condition and the 2 rules for each subfolder?
How would I allow subfolders of the subfolder to display? Currently it is throwing a 404 at me... :(
============
Still need help with follow-up question #1, but I believe I got question #2.
SOLUTION (I believe) to follow-up question #2:
RewriteCond %{THE_REQUEST} /_plugin/notifications/ [NC]
RewriteRule ^_plugin/notifications/(.*)$ /notifications/$1 [L,R]
RewriteRule ^notifications/(.*)$ /_plugin/notifications/$1 [L]
None of the rules you have tried are correct.
To redirect /folder/subfolder to /root/subfolder you need a permanent 301 Redirect rule something like the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} /folder/subfolder/ [NC]
RewriteRule ^folder/subfolder/$ /subfolder/ [L,R]
The above will redirect http://example.com/folder/subfolder/ to http://example.com/subfolder .
You will get a 404 error if the /subfolder/ doesn't exist in the root dir. To avoid the 404 error you can rewrite the /subfolder/ uri back to its original location /folder/subfolder/ using an internal Rewrite just bellow the first one
:
RewriteRule ^subfolder/?$ /folder/subfolder/ [L]

Rewrite URL to get contents from sub-directory

I have edited my /etc/hosts file to link domain.com to my localhost. Now when I visited domain.com I want it to get the site contents from domain.com/site/ but only using the URL domain.com thus removing site/ from the URL.
I have done this once before, so I know it's possible, but I lost the code. Can someone please assist me? .htaccess scripting is not my strong. Apologies if this is a duplicate of something else.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!site/).*)$ site/$1 [L,NC]
I have managed to get this working with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)your_directory
RewriteRule ^(.*)$ your_directory/$1 [L]

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

Redirecting Subdomain to Subfolder

I try to add the following logic to a .htaccess file:
subdomain.domain.xx make a redirect to subdomain.domain.xx/subdomain/
xy.domain.xy redirect to xy.domain.xy/xy/ (it should work with every subdomain without adding new rewrites)
i found a lot of solutions to redirect subdomain.domain.xx -> domain.xx/subdomain, but nothing like i need... below you find the code i try out.
Hope someone can help me. Thanks.
RewriteEngine on
RewriteBase /
RewriteRule ^([^.?]+[^.?/])$ $1/ [R]
RewriteCond %{HTTP_HOST} ^(www)?.domain.ch/$
RewriteRule .* http\://%1$1.domain.ch/%1$1 [I,R=301]
Place this in the .htaccess file of your subdomain's root directory:
DirectoryIndex index.html
RewriteEngine on
Redirect permanent /index.html http://subdomain.domain.xx/subdomain/

Create a 301 redirect in .htaccess for folder to subdomain AND redirect www to non-www

I've searched everywhere and used some examples but they don't meet my specific needs, hence why I'm asking here if anyone can please help? I know the http:: below isn't correct, it's just because I can't post links here.
I'd like to redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
whilst also being able to redirect
http:://www.mysite.co.uk/mysub/
and 2. http:://mysite.co.uk/mysub/
to 3. http:://mysub.mysite.co.uk/
All files in 1. and 2. should be redirected to their equivelant in 3.
For example: http:://www.mysite.co.uk/mysub/file.html AND http:://mysite.co.uk/mysub/file.html should both go to http:://mysub.mysite.co.uk/file.html
This is to affect the subdomain only, so that other folders and files in the site root aren't affected.
If anyone could help me understand and write the code for the 301 redirect in a .htaccess file I'd really really appreciate it! Thanks!
This is the code you'll need in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]

Resources