htaccess remove subdirectory from url

2019-09-06 10:49发布

I have read multiple threads based on this subject but none of the rules have worked so i'm sure i m doing something wrong.

I have the following struncture in my /var/www

main domain folders that redirects to domain.com
folder1 that redirects to domain1.com
folder2 that redirects to domain2.com

Inside folder2 i have 3 more subdirectories

folder2/subdirectory1
folder2/subdirectory2
folder2/subdirectory3

These should redirect to:

www.domain2.com/subdirectory1
www.domain2.com/subdirectory2
www.domain2.com/subdirectory3

but instead of that it also shows the folder2 in the url so it's listing

www.domain2.com/folder2/subdirectory1
www.domain2.com/folder2/subdirectory2
www.domain2.com/folder2/subdirectory3

I have two .htaccess: one inside the root folder and one inside folder2

htaccess from root folder

Options +FollowSymLinks 
RewriteEngine On
RewriteBase /

ReWriteCond %{HTTP_HOST} domain1.com
ReWriteCond %{REQUEST_URI} !folder1/
ReWriteRule ^(.*)$ folder1/$1 [L] 

ReWriteCond %{HTTP_HOST} domain2.com
ReWriteCond %{REQUEST_URI} !folder2/
ReWriteRule ^(.*)$ folder2/$1 [L]

htaccess from folder2

RewriteEngine On
RewriteBase / 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory1
ReWriteCond %{REQUEST_URI} !subdirectory1/
ReWriteRule ^(.*)$ subdirectory1/$1 [L] 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory2
ReWriteCond %{REQUEST_URI} !subdirectory2/
ReWriteRule ^(.*)$ subdirectory2/$1 [L] 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory3
ReWriteCond %{REQUEST_URI} !subdirectory3/
ReWriteRule ^(.*)$ subdirectory3/$1 [L]

Hopefully i can finally fix this problem

1条回答
家丑人穷心不美
2楼-- · 2019-09-06 11:29

I think you should use a redirect tag [R] into folder2 .htaccess RewriteRule.

This way:

RewriteEngine On
RewriteBase / 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory1
ReWriteCond %{REQUEST_URI} !subdirectory1/
ReWriteRule ^(.*)$ subdirectory1/$1 [R] 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory2
ReWriteCond %{REQUEST_URI} !subdirectory2/
ReWriteRule ^(.*)$ subdirectory2/$1 [R] 

ReWriteCond %{HTTP_HOST} domain2.com/subdirectory3
ReWriteCond %{REQUEST_URI} !subdirectory3/
ReWriteRule ^(.*)$ subdirectory3/$1 [R]
查看更多
登录 后发表回答