Wildcard subdomain to subfolder in another subfolder - .htaccess

I want to redirect sudomains to subfolders with a .htaccess
Working:
subdomain.example.com -> example.com/subdomain ✓
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ %1/$1 [L,QSA]
Now I want to rewrite the subdomain in another subfolder:
Working, but with a redirect:
subdomain.example.com -> example.com/subfolder/subdomain ✓
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ https://example.com/subfolder/%1/$1 [L,QSA]
Not Working:
subdomain.example.com -> example.com/subfolder/subdomain x
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ subfolder/%1/$1 [L,QSA]
Why cant i add another subfolder? The subfolder is defenitely available (redirect working)

You may try this rule for subfolder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/subfolder/\1/ [NC]
RewriteRule ^(.*)$ subfolder/%1/$1 [L,QSA]

Related

htaccess Redirect non.www and www. to a subfolder

I tried different ways in htaccess but none worked. I have 2 domains and 1 webshosting.
I want that any user who types in website_1.com or www.website_1.com should be redirected to www.website_1.com/abc and any user who types website_2.com or www.website_2.com should be redirected to www.website_2.com/xyz
How could I put that in one single htaccess file? My current htaccess file:
#Redirect from non-www. to www.
RewriteCond %{HTTP_HOST} ^website_1\.com$ [NC]
RewriteRule ^(.*)$ http://www.website_1.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website_2\.com$ [NC]
RewriteRule ^(.*)$ http://www.website_2.com/$1 [R=301,L]
#Redirect specific Domain to subfolder
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website_1\.com [NC]
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule ^(.*)$ /abc/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website_2\.com [NC]
RewriteCond %{REQUEST_URI} !^/xyz/.*$
RewriteRule ^(.*)$ /xyz/$1

Htaccess redirect from subdomain, but not it's folders

I have
sub.domain.com and want it to be redirected to domain.com
But there must not be redirects from e.g. sub.domain.com/folder/ or sub.domain.com/folder/index.html
Everything i've tried does not work...
For example
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/folder/?$
RewriteCond %{REQUEST_URI} !^/folder/index\.html$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]

Redirect www to non www with subfolder and links

Ok, so i want to redirect all www to non www link,
I want to redirect www.example.com to example.com, and i can do that too using this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
But when someone try to access www.example.com/example, that redirects the site to example.com,
I want a code that can redirect www.example.com/example or any subfolder to example.com/example or any subfolder.
Kinda like a wildcard entry, how to do that?
Also if possible force ssl on it too.
Thank You.
If I've understood your question correctly:
www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]
OR Use:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/subfoldername/$1 [R=301,L]
-------- Alternative---------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(/)?$ /subfolder/index.php [L]
To force SSL:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

htaccess rewrite rule for subdomains with page id

i want to redirect
http://subdomain.example.com/index.php?pageID=45
to
http://example.com/subdomain/45.html
Please help me with this
i tried
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.example.com/sudomain/$1 [L,R=301]
You can replace your code by this one in your htaccess (in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^pageID=([1-9][0-9]*)$ [NC]
RewriteRule ^index\.php$ http://example.com/subdomain/%1.html? [R=301,L]

htaccess redirect subdomain to folder and non-www to www

sub.domain.com redirect to www.domain.com/sub redirects using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*)$ "http://www.domain.com/sub/$1" [R=301,L]
domain.com/sub to www.domain.com/sub is where I'm stuck.The solutions I've found always drop the /sub/ folder when rewriting the www.
This works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/sub/$1 [R=301,L]

Resources