Htaccess Redirect from subdirectory to root - .htaccess

I'm looking for a htaccess code to redirect my url's like this :
http://01.mydomain.com/subdir/xyz [OR]
http://www.01.mydomain.com/subdir/xyz [OR]
http://02.mydomain.com/subdir/xyz [OR]
http://www.02.mydomain.com/subdir/xyz
TO : http://www .mydomain.com/xyz
and in this case xyz is dynamic and could be any value. and subdir is constant

Assuming subdir is constant, the following is one way:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^01.mydomain.com [OR]
RewriteCond %{HTTP_HOST} ^www.01.mydomain.com [OR]
RewriteCond %{HTTP_HOST} ^02.mydomain.com [OR]
RewriteCond %{HTTP_HOST} ^www.02.mydomain.com
RewriteRule ^subdir/(.*)$ http://www.mydomain.com/$1 [L,R=301]

RewriteRule ^/subdir/(.*)/?$ /$1 [R=301,L]

Related

How to point primary domain to subdir and remove subdirectory from URL with htaccess?

My primary domain on my webhosting I am having point to a subdirectory within public_html via htaccess like so:
RewriteCond %{THE_REQUEST} ^GET\ /apt/
RewriteCond %{HTTP_HOST} ^(www\.)?mysite.com$
RewriteRule ^apt/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite.com$
RewriteRule !^apt/ apt%{REQUEST_URI} [L]
However, within that subdirectory, all URLs include the subdirectory (which is the domain). So it's domain.com/domain.com/page. Locally I use AMPPS and so the URLs are localhost/domain.com/page. Something I should fix...
How do I remove the subdirectory from URL paths? Do I do so in the htaccess within the subdirectory? Thanks.
UPDATE: Now after plenty of messing around, the root htaccess has:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?my-web.agency$
RewriteRule !^my-web.agency/ my-web.agency/%{REQUEST_URI} [L,NC]
URLs work with and without the subdirectory in URL; however I want to disallow the subdirectory from the URI.
Also every link auto-generated by ProcessWire includes the subdir in URI.
Inside the subdir my-web.agency is a ProcessWire install (think WordPress for this case) with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
AddDefaultCharset UTF-8
RewriteRule "(^|/)\.(?!well-known)" - [F]
RewriteCond %{REQUEST_URI} !(^|/)site-[^/]+/install/[^/]+\.(jpg|jpeg|png|gif)$
RewriteCond %{REQUEST_URI} (^|/)\.htaccess$ [NC,OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/assets/(cache|logs|backups|sessions|config|install|tmp)($|/.*$) [OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/install($|/.*$) [OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/assets.*/-.+/.* [OR]
RewriteCond %{REQUEST_URI} (^|/)(wire|site|site-[^/]+)/(config|index\.config|config-dev)\.php$ [OR]
RewriteCond %{REQUEST_URI} (^|/)(wire|site|site-[^/]+)/templates-admin($|/|/.*\.(php|html?|tpl|inc))$ [OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/templates($|/|/.*\.(php|html?|tpl|inc))$ [OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/assets($|/|/.*\.php)$ [OR]
RewriteCond %{REQUEST_URI} (^|/)wire/(core|modules)/.*\.(php|inc|tpl|module|info\.json)$ [OR]
RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/modules/.*\.(php|inc|tpl|module|info\.json)$ [OR]
RewriteCond %{REQUEST_URI} (^|/)(COPYRIGHT|INSTALL|README|htaccess)\.(txt|md|textile)$ [OR]
RewriteCond %{REQUEST_URI} (^|/)site-default/
RewriteRule ^.*$ - [F,L]
RewriteCond %{REQUEST_URI} "^/~?[-_.a-zA-Z0-9/]*$"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php?it=$1 [L,QSA]
How do I remove the subdirectory from the URL? Please?

.htaccess redirect https://www to https://non-www

i have a 301 redirect from all domains to https://example.de.
The redirect works with http://www.example, www.example with all tld (.com,.eu.net).
But with https://www.example it doesnt redirect to https without the www.
Here is the Mod rewrite:
RewriteEngine on
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^www\.example\.de$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.at$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.at$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.eu$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.eu$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Edit:
The following works so far
RewriteCond %{HTTPS} on [NC]
RewriteCond %{HTTP_HOST} !^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteBase /
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Your rule doesn't redirect https urls because you are not using correct Rewrite Conditions. Also you dont need to write multiple conditions to test a domain name. Instead of using two RewriteCond lines to test www.example.com and example.com you can do it in a single condition something like. RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ .
The following is the shorter version of your rules.
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.at$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [OR]
# the main domain. redirect www.example.de to example.de
RewriteCond %{HTTP_HOST} ^www\.example\.at$
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Clear your browser cache before testing this change.

how to redirect www.xyz.com/webmail to mail.xyz.com through .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^webmail$ "http\:\/\/mail\.domain\.com\/" [R=301,L]
But Above code in .htaccess is not working however given below code is working
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^folderName\/$ "http\:\/\/www\.domain\.net\/" [R=301,L]
Try using this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.xyz.com
RewriteRule ^webmail/(.*)$ http://mail.xyz.com/$1 [L,R=301]
EDIT:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$
RewriteRule ^webmail/(.*) http://mail.xyz.com/$1 [R=301,L]

How can I create virtual subdomain which points to a folder?

I am trying to create a website that would create its portfolio, currently once created your page that has the URL domain.com/content/username but I would like to create a subdomain for each user therefore username.domain.com. Do you have any idea how to do this? I tried this:
Options +FollowSymLinks -Multiviews
RewriteEngine on
RewriteBase /
#
# Canonicalize the hostname
RewriteCond www.%{HTTP_HOST} ^(www)\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(mine-app\.fr) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(mine-app\.fr). [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(mine-app\.fr):[0-9]+
RewriteRule (.*) http://%1.%2/$1 [R=301,L]
#
# If subdomain is NOT www
RewriteCond %{HTTP_HOST} !^www\.mine-app\.fr [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.mine-app\.fr$
# Rewrite if requested URL resolves to existing file or subdirectory in /content/<content>/ path
RewriteCond %{DOCUMENT_ROOT}/content/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/content/%1/$1 -d
RewriteRule (.*) /content/%1/$1 [L]
But nothing worked. How can I solve this?

How to write a htaccess file to wildcard 2 level folder to subdomain

How to rewrite this
www.mywebsite.com/folder/folder1 to folder1.mywebsite.com/
www.mywebsite.com/folder/folder2 to folder2.mywebsite.com/
www.mywebsite.com/folder/folder3 to folder3.mywebsite.com/
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.mywebsite\.com$
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^((?!folder/).*)$ /folder/%1/$1 [L]

Resources