4

Подскажите как сделать так, чтобы при заходе по адресу http://site.com/sub или http://www.site.com/sub переадресовывало на http://sub.site.com?
В корневом каталоге располагается файл .htaccess с содержимым

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 400 /index.php?route=error/_400
ErrorDocument 401 /index.php?route=error/_401
ErrorDocument 403 /index.php?route=error/_403
ErrorDocument 404 /index.php?route=error/_404
ErrorDocument 500 /index.php?route=error/_500
RewriteCond %{HTTP_HOST} ^site\.com$
RewriteRule ^sub/(.*)$ http://sub.site.com/$1 [L,R=301]
RewriteRule ^sub$ http://sub.site.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]

В папке sub располагается файл .htaccess с похожим содержимым

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 400 /index.php?route=error/_400
ErrorDocument 401 /index.php?route=error/_401
ErrorDocument 403 /index.php?route=error/_403
ErrorDocument 404 /index.php?route=error/_404
ErrorDocument 500 /index.php?route=error/_500
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

Так же имеется вопрос, можно ли как то из папки sub в файле .htaccess обратиться к файлу из корневой директории, например, RewriteRule ^style/([@.A-Za-z0-9-/+_=]+)$ ../library/styles/$1 [L] ?

Пытаюсь сделать следующее

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 400 /index.php?route=error/_400
ErrorDocument 401 /index.php?route=error/_401
ErrorDocument 403 /index.php?route=error/_403
ErrorDocument 404 /index.php?route=error/_404
ErrorDocument 500 /index.php?route=error/_500
#
RewriteCond %{ENV:REDIRECT_VHOST} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.site\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sub/index.php?route=$1 [L,E=VHOST:1]
#
RewriteCond %{HTTP_HOST} ^site\.com$
#RewriteRule ^sub/(.*)$ http://sub.site.com/$1 [L,R=301]
#RewriteRule ^sub$ http://sub.site.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

Не работает перенаправление с site.con/sub на sub.site.com (циклическая переадресация), а так же при переходе, например, на sub.site.com/abc - ошибка 500, кто знает, что нужно исправить?

2
  • 2
    Простите за оффтоп. Но вот почитать про mod_rewrite habrahabr.ru/company/sprinthost/blog/129560
    – Skim
    28 авг 2015 в 22:25
  • Благодарю за ответ, но мне нужно чтобы правила для поддомена были записаны в корневом .htaccess.
    – Cript
    1 сен 2015 в 12:03

2 ответа 2

1

В файле .htaccess нужно прописать:

Redirect 301 /sub http://sub.site.com
3
  • а зачем первые две строчки?
    – mymedia
    8 авг 2017 в 12:40
  • на всякий случай, чтобы потом добавить что либо еще
    – paqstd
    8 авг 2017 в 12:42
  • полагаю, в таком случае имеет смысл описать ваши намерения и рассказать, как работает этот кусочек конфигурации
    – mymedia
    8 авг 2017 в 12:46
0

Не рекомендовал бы использовать в домене www, его сейчас мало кто использует:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\..* [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]

И решение Вашей проблемы будет выглядеть так:

Redirect /sub http://sub.site.com
Redirect http://www.site.com/sub http://sub.site.com

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.