C
C
Cheizer2019-11-20 17:21:18
htaccess
Cheizer, 2019-11-20 17:21:18

How to make a 301 redirect from a folder to a subdomain using Rewrite in htacess?

Friends, help me make the right 301 redirect using Rewrite.
The situation is as follows, on the conditional site.ru domain, the registrar has A records *.site.ru and www.site.ru and site.ru for the IP ares of the server, this is for linking the domain and all subdomains, this is for the full picture.
Subdomains are created automatically, by creating folders, there is a folder called subdomains in the root, subdomain folders are created in it, for example, there is a test folder, and if you go to test.site.ru, then everything is in order, the subdomain works from the desired folder. But here's the problem, if you go to the site.ru/subdomains/test/ URL, it redirects to the main site.ru domain, but you need to go to the subdomain, why is this happening? What's wrong with htaccess rules?
Here is my htaccess at the root of the site.

RewriteEngine On
RewriteCond %{THE_REQUEST} /subdomains/[^/]
RewriteRule ^subdomains/[^/]+/(.*) /$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^(.+)\.site\.ru$ [NC]
RewriteCond %{HTTP_HOST} !^www\.site\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains
RewriteRule ^(.*) %{HTTP_HOST}$1
RewriteRule ^(www\.)?(.+)\.site\.ru(.*) /subdomains/$2%{REQUEST_URI} [L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

AddDefaultCharset utf-8
AddCharset utf-8 *
<IfModule mod_charset.c>
CharsetSourceEnc utf-8
CharsetDefault utf-8
</IfModule>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-11-20
@Cheizer

RewriteCond %{THE_REQUEST} /subdomains/[^/]
RewriteRule ^subdomains/[^/]+/(.*) /$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^(.+)\.site\.ru$ [NC]
RewriteCond %{HTTP_HOST} !^www\.site\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains
RewriteRule ^(.*) %{HTTP_HOST}$1
RewriteRule ^(www\.)?(.+)\.site\.ru(.*) /subdomains/$2%{REQUEST_URI} [L]

RewriteCond %{THE_REQUEST} /subdomains/
RewriteRule ^subdomains/([^/]+)/(.*) http://$1.site.ru/$2 [R=301,L]

RewriteCond %{HTTP_HOST} ^(?>www\.|)(.+)\.site\.ru$   [NC]
RewriteRule !^subdomains /subdomains/%1%{REQUEST_URI} [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question