我非常感谢任何人对mod_rewrite问题的帮助。我不知道正则表达式并且不熟悉.htaccess指令,所以我想不出如何解决我遇到的问题。
以下是我想要完成的内容的描述......
3个网站:
http://www.example.com/ or http://example.com/
http://east.example.com/
http://west.example.com/
主网站上的页面需要重定向到其他网站:
http://www.example.com/location1
http://www.example.com/location2
http://www.example.com/location3
http://www.example.com/location4
http://www.example.com/location5
http://www.example.com/location6
需要根据位置将某些网页重定向到一个网站,例如http://east.example.com/:
http://www.example.com/location1 redirect to http://east.example.com/location1
http://www.example.com/location2 redirect to http://east.example.com/location2
http://www.example.com/location3 redirect to http://east.example.com/location3
(also without www) e.g.
http://example.com/location1 redirect to http://east.example.com/location1
需要根据位置将某些网页重定向到其他网站,例如http://west.example.com/:
http://www.example.com/location4 redirect to http://west.example.com/location4
http://www.example.com/location5 redirect to http://west.example.com/location5
http://www.example.com/location6 redirect to http://west.example.com/location6
(also without www) e.g.
http://example.com/location4 redirect to http://west.example.com/location4
想要一个类似于:
的重写规则If domain name is www.example.com or domain name is example.com
And domain name is not east.example.com
And domain name is not west.example.com
And "somelocation" (in the east) is in the URL
Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
注意:这三个网站实际上共享相同的代码库,因此URL都指向相同的位置/ .htaccess文件。所以,我尝试使用基本的重定向,并最终得到一个错误,说...由于重定向太多而无法打开页面。
如果有人知道答案并且可以帮助解决这个问题,我真的很感激帮助!
编辑:预期结果的示例
原始网址: “http://www.example.com/locations/eastend-web-page”
重写网址: “http://east.example.com/locations/eastend-web-page”
答案 0 :(得分:0)
RewriteEngine On
# If domain name is www.example.com or domain name is example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
# And "somelocation" (in the east) is in the URL,
# Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
RewriteRule ^(.*somelocation.*)$ http://east.example.com/$1 [R,L]
你需要为西方做同样的事情。请注意,如果域名是www.example.com或example.com,则无法为east.example.com或west.example.com
答案 1 :(得分:0)
请尝试填写重写地图文件(请参阅here),以便与属于“东部”域的网址相对应。你可以这样声明:
RewriteMap mapmaintoeast \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintoeast.map
关于您的示例,您的地图文件可能会填充以下内容:
eastlocation1 mainlocation1
eastlocation2 mainlocation2
eastlocation3 mainlocation3
...
我称他们为“东方”和“主要”以便清楚地区分。这些示例应该是URL。
为西方做同样的事情:
RewriteMap mapmaintowest \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintowest.map
在该文件中:
westlocation1 mainlocation4
westlocation2 mainlocation5
westlocation3 mainlocation6
...
然后在这里你去寻找困难的部分:
# This cond put any non-empty string into "%1" (= parenthesis + first cond):
RewriteCond %{QUERY_STRING} (.+)
# The following rule doesn't touch the URL, but
# will try to search into the map file and
# create fill an environment variable called MAINTOEAST with the
# string found and if not found, assign MAINTOEAST to "notfound"
RewriteRule . - [QSA,E=MAINTOEAST:${mapmaintoeast:%1|notfound}]
# if MAINTOEAST not empty and different from "notfound":
RewriteCond %{ENV:MAINTOEAST} !^$
RewriteCond %{ENV:MAINTOEAST} !(notfound)
# ok found => redirect to east:
RewriteRule (.*) http://east.example.com$1 [QSA,R=301,L]
# Now do the same with west (no comments needed (see previous)):
RewriteCond %{QUERY_STRING} (.+)
RewriteRule . - [QSA,E=MAINTOWEST:${mapmaintowest:%1|notfound}]
RewriteCond %{ENV:MAINTOWEST} !^$
RewriteCond %{ENV:MAINTOWEST} !(notfound)
RewriteRule (.*) http://west.example.com$1 [QSA,R=301,L]
这应该有效。 我希望我已经给你足够的线索来完成这项工作;) 如果你没有足够的线索......
请尝试使用RewriteLog
指令:它可以帮助您找出此类问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
我最喜欢检查regexp的工具:
http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)