Apache mod_rewrite: how to redirect addon domains subfolder access to addon domain itself?

让人想犯罪 __ 提交于 2019-12-13 02:59:38

问题


Situation (directories tree) on an Apache server:

maindomain.com/
|
|_ .htaccess (just an empty file, no rule in here)
|
|_ addondomain1.com/
|  |
|  |_ .htaccess
|  |_ index.html
|
|_ addondomain2.com/
   |
   |_ .htaccess
   |_ index.html

Currently files in addondomain1.com can be viewed by going to:

http://www.addondomain1.com/index.html
http://www.addondomain1.com/
http://www.addondomain1.com
http://www.maindomain.com/addondomain1.com/index.html

I would like to redirect all requests:

http://maindomain/addondomain1.com/some/path/anypage.html (with or without 'www')

to same path/file but under addondomain1.com and always with 'www':

http://www.addondomain1.com/some/path/page.html

In order to try to accomplish this, I placed in maindomain.com/addondomain1.com/.htaccess this rule:

RewriteCond %{HTTP_HOST} !^www\.addondomain1\.com$ 
RewriteRule ^(.*)$ "http://wwww.addondomain1.com/$1" [R=301,L]

This one works almost great, and redirects well all the ones below:

http://addondomain1.com/index.html >> http://www.addondomain1.com/index.html
http://www.addondomain1.com/ >> http://www.addondomain1.com/
http://www.addondomain1.com  >> http://www.addondomain1.com
http://www.maindomain.com/addondomain1.com/index.html >> http://www.addondomain1.com/index.html
http://www.maindomain.com/addondomain1.com/ >> http://www.addondomain1.com/
http://maindomain.com/addondomain1.com/ >> http://www.addondomain1.com/

But unfortunately when going to:

http://maindomain.com/addondomain1.com
http://www.maindomain.com/addondomain1.com

NOTE both links above are WITHOUT final slash, it redirects to:

http://www.addondomain1.com//server/root/path/addondomain1.com

I think the regexp ^(.*)$ is getting "addondomain1.com" as part because the final slash is missing. Do you know how fix/workaorund this issue?

Thanks!


回答1:


DirectorySlash Off



回答2:


Try this:

RewriteCond %{HTTP_HOST} !^www\.addondomain1\.com$
RewriteCond %{REQUEST_URI} ^/[^/]+/(.*)
RewriteRule ^ http://wwww.addondomain1.com/%1 [R=301,L]


来源:https://stackoverflow.com/questions/3242322/apache-mod-rewrite-how-to-redirect-addon-domains-subfolder-access-to-addon-doma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!