apache 下的 htaccess 转换为 nginx 和 iis

2016-07-13 14:36:48 +08:00
 veking

试过在线的 htaccess 转换服务,基本都不行,这个问题困扰很久了,期待高人指点!

重写规则实现的是多级子目录转换为二级域名绑定,如 http://www.aaa.com/news/a1/b5/xc/admin/index.php?id=1 转换为 http://shop.aaa.com/index.php?id=1 访问

以下为 apache 的重写规则

RewriteEngine on

RewriteCond %{HTTP_HOST} ^shop.aaa.com$

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteBase /

RewriteRule . /index.php [L]

RewriteCond %{HTTP_HOST} ^shop.aaa.com$

RewriteCond %{REQUEST_URI} !^/news/a1/b5/xc/admin

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /news/a1/b5/xc/admin/$1

RewriteCond %{HTTP_HOST} ^shop.aaa.com$

RewriteRule ^(/)?index.php$ news/a1/b5/xc/admin/index.php [L]

2053 次点击
所在节点    程序员
12 条回复
veking
2016-07-13 14:39:15 +08:00
其中二级目录 news/a1/b5/xc/admin/ 为固定内容和格式
aprikyblue
2016-07-13 19:28:04 +08:00
if ($host = "shop.aaa.com") {
if (!-e $request_filename) {
rewrite . /index.phplast;
}
if ($request_uri !~ "^/news/a1/b5/xc/admin") {
rewrite ^(.*)$ /news/a1/b5/xc/admin/$1;
}
rewrite ^(/)?index.php$ news/a1/b5/xc/admin/index.php last;
}

没测试。。
aprikyblue
2016-07-13 19:29:27 +08:00
楼上是 nginx ,呃,第三行最后的 last 前面少了个空格。。
veking
2016-07-13 20:24:50 +08:00
@aprikyblue
哇,果然是高手!

还有一点小问题,如何把这种( http://shop.aaa.com/static/images/a.jpg )请求过滤掉,不要重写到 news/a1/b5/xc/admin/index.php 文件
aprikyblue
2016-07-13 22:02:03 +08:00
你觉得为什么这么简单的事情, 100 多次点击,别人连回复都懒得回?
lslqtz
2016-07-14 01:24:00 +08:00
@aprikyblue 感谢已发送。
@veking 为啥不再单独开个 server 块呢。。
我虚拟主机用这个,不过不是这段。。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.myloli.club$ [NC]
RewriteRule ^(.*)$ http://myloli.club/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^myloli.club$ [NC]
RewriteCond %{REQUEST_URI} !^/myloliclub/
RewriteRule ^(.*)$ myloliclub/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)233.dog$ [NC]
RewriteRule ^(.*)$ http://hloli.club/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)hloli.club$ [NC]
RewriteCond %{REQUEST_URI} !^/myloliclub/data/
RewriteRule ^(.*)$ myloliclub/data/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)rori(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/comingsoon/
RewriteRule ^(.*)$ comingsoon/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^myipass.tk$ [NC]
RewriteRule ^(.*)$ http://www.myipass.tk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.myipass.tk$ [NC]
RewriteCond %{REQUEST_URI} !^/mypass/
RewriteRule ^(.*)$ mypass/$1 [L,QSA]
veking
2016-07-14 09:06:45 +08:00
@aprikyblue 这个问题困扰我将近一个月了,网上搜索了很多资料都没搞明白,自己也花时间研究了一下 nginx 的重写规则,最后也没搞明白,猪八戒上托管佣金浪费了一个星期,最后服务商解决不退款了……

可能这个问题真的很简单吧,但是我真没搞明白,谢谢你第一个回复!
aprikyblue
2016-07-14 09:15:41 +08:00
aprikyblue
2016-07-14 09:43:01 +08:00
@veking
好吧,的确需要一点储备。。想起当年自己啥也不会纠结这东西哈哈

就帮你写了吧,是过滤 /static/吧?不是就自己改一下,,

if ($host = "shop.aaa.com") {
if (!-e $request_filename) {
rewrite . /index.php last;
}
if ($request_uri ~ "^/static/") {
break;
}
if ($request_uri !~ "^/news/a1/b5/xc/admin") {
rewrite ^(.*)$ /news/a1/b5/xc/admin/$1;
}
rewrite ^(/)?index.php$ news/a1/b5/xc/admin/index.php last;
}


给你点建议。。
如果你是有哪里不明白更详细些说出来哪里不明白,也许乐意教你的更多。。
或者你愿意给报酬也行。毕竟别人的时间也是时间。
像你这主题写的直接给要求,就伸手党似得。。估计这里喜欢的人不会很多
veking
2016-07-14 09:54:25 +08:00
@aprikyblue 万分感谢!
看了你解决方式,貌似有点新思路了,还是对重写规则不太熟悉,需要继续学习研究。
留个微信发个红包聊表心意!

ps :以后会改进一下提问方式,感谢真诚相告!
aprikyblue
2016-07-14 10:29:16 +08:00
@veking eastphoton
veking
2016-07-14 10:49:04 +08:00
终于搞定,把最终配置发出来,供大家参考

set $flag 0;
if ($host = "shop.abc.com") {
set $flag "${flag}1";
}
if ($request_uri ~ "^(/+)(attachment|static)/") {
set $flag "${flag}2";
}
if ($flag = "012") {
break;
}
if (!-e $request_filename) {
set $flag "${flag}3";
}
if ($flag = "013") {
rewrite . /index.php last;
}
if ($request_uri !~ "^/news/a1/b5/xc/admin") {
set $flag "${flag}4";
}
if ($flag = "014") {
rewrite ^(.*)$ /news/a1/b5/xc/admin/$1;
}
if ($flag = "01") {
rewrite ^(/)?index.php$ news/a1/b5/xc/admin/index.php last;
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/292226

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX