web.config 转 .htaccess
web.config 转 .htaccess
web.config 规则如图,但是这个规则只能在WINDOWS下IIS中使用,我们想要在LINUX中使用规则,需要把这个转换成 .htaccess规则
工具 python 下载地址:https://www.python.org/downloads/
python转换脚本
import re with open("Web.config",encoding="utf-8") as f: txt=f.read() with open(".htaccess","w+") as f: ref=re.findall(r"""<rule name.+?match url="(.+?)".+?url="(.+?)".+?</rule>""",txt,16) results=["RewriteRule "+r[0]+"$ "+r[1].replace("{R:1}","$1").replace("{R:2}","$2")+" [L]\n" for r in ref] print(results) f.writelines(results)
web.config规则文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="已导入的规则 1"> <match url="^index.html$" /> <conditions logicalGrouping="MatchAll"> <add input="{DOCUMENT_ROOT}{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> <add input="{DOCUMENT_ROOT}{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="index.php" appendQueryString="true" /> </rule> <rule name="已导入的规则 2"> <match url="^404.html$" /> <action type="Rewrite" url="index.php?service=siteConfig&template=404" appendQueryString="true" /> </rule> <rule name="已导入的规则 3"> <match url="^about.html$" /> <action type="Rewrite" url="index.php?service=siteConfig&template=about" appendQueryString="true" /> </rule> <rule name="已导入的规则 4"> <match url="^about-(\d+).html$" /> <action type="Rewrite" url="index.php?service=siteConfig&template=about&id={R:1}" appendQueryString="true" /> </rule> <rule name="已导入的规则 5"> <match url="^mobile.html$" /> <action type="Rewrite" url="index.php?service=siteConfig&template=mobile" appendQueryString="true" /> </rule> <rule name="已导入的规则 6"> <match url="^protocol-(\d+).html$" /> <action type="Rewrite" url="index.php?service=siteConfig&template=protocol&id={R:1}" appendQueryString="true" /> </rule> <rule name="已导入的规则 7"> <match url="^complain-(\w+)-(\w+)-(\d+).html$" /> <action type="Rewrite" url="index.php?service=member&template=complain&module={R:1}&dopost={R:2}&aid={R:3}" appendQueryString="true" /> </rule> <rule name="已导入的规则 8"> <match url="^login_popup.html$" /> <action type="Rewrite" url="index.php?service=member&template=login_popup" appendQueryString="true" /> </rule> <rule name="已导入的规则 9"> <match url="^login.html$" /> <action type="Rewrite" url="index.php?service=member&template=login" appendQueryString="true" /> </rule> <rule name="已导入的规则 10"> <match url="^loginCheck.html$" /> <action type="Rewrite" url="index.php?service=member&template=loginCheck" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
执行convert.py脚本,会在同级目录下生成.htaccess规则文件。
.htaccess 规则文件代码
RewriteRule ^index.html$$ index.php [L] RewriteRule ^404.html$$ index.php?service=siteConfig&template=404 [L] RewriteRule ^about.html$$ index.php?service=siteConfig&template=about [L] RewriteRule ^about-(\d+).html$$ index.php?service=siteConfig&template=about&id=$1 [L] RewriteRule ^mobile.html$$ index.php?service=siteConfig&template=mobile [L] RewriteRule ^protocol-(\d+).html$$ index.php?service=siteConfig&template=protocol&id=$1 [L] RewriteRule ^complain-(\w+)-(\w+)-(\d+).html$$ index.php?service=member&template=complain&module=$1&dopost=$2&aid={R:3} [L] RewriteRule ^login_popup.html$$ index.php?service=member&template=login_popup [L] RewriteRule ^login.html$$ index.php?service=member&template=login [L] RewriteRule ^loginCheck.html$$ index.php?service=member&template=loginCheck [L]
需要在生成好的htaccess文件头和尾增加固定规则代码即可搞定,如下
头部添加规则
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / ErrorDocument 404 /404.html RewriteCond %{REQUEST_METHOD} ^(OPTIONS) RewriteRule .* - [F] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f
尾部添加规则
</IfModule>
最终htaccess规则文件完整代码如下
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / ErrorDocument 404 /404.html RewriteCond %{REQUEST_METHOD} ^(OPTIONS) RewriteRule .* - [F] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^index.html$$ index.php [L] RewriteRule ^404.html$$ index.php?service=siteConfig&template=404 [L] RewriteRule ^about.html$$ index.php?service=siteConfig&template=about [L] RewriteRule ^about-(\d+).html$$ index.php?service=siteConfig&template=about&id=$1 [L] RewriteRule ^mobile.html$$ index.php?service=siteConfig&template=mobile [L] RewriteRule ^protocol-(\d+).html$$ index.php?service=siteConfig&template=protocol&id=$1 [L] RewriteRule ^complain-(\w+)-(\w+)-(\d+).html$$ index.php?service=member&template=complain&module=$1&dopost=$2&aid={R:3} [L] RewriteRule ^login_popup.html$$ index.php?service=member&template=login_popup [L] RewriteRule ^login.html$$ index.php?service=member&template=login [L] RewriteRule ^loginCheck.html$$ index.php?service=member&template=loginCheck [L] </IfModule>
web.config规则转换 转 .htaccess
时间:2018-08-25
制作:王宝军