将IIS web.configredirect规则转换为Apache .htaccess

我需要将一个旧的PHP站点从Windows IIS移动到Linux Apache服务器。 有一些redirect规则,我必须转换。 其中一部分我已经pipe理,但对于其他一些规则我缺乏理解,使他们的工作。

有原始的web.config规则:

<rule name="Rule1" enabled="true" stopProcessing="true"> <match url="^switch\.php$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> <add input="{QUERY_STRING}" pattern="^lang=([^=&amp;]+)$" /> </conditions> <action type="Redirect" url="switch/{C:1}" appendQueryString="false" /> </rule> <rule name="Rule2" enabled="true" stopProcessing="true"> <match url="^switch/([^/]+)/?$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="switch.php?lang={R:1}" /> </rule> <rule name="Rule3" stopProcessing="true"> <match url="^zone-client/index\.php$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> </conditions> <action type="Redirect" url="zone-client/index" appendQueryString="false" /> </rule> <rule name="Rule4" stopProcessing="true"> <match url="^zone-client/index$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="zone-client/index.php" /> </rule> <rule name="Rule5" stopProcessing="true"> <match url="^index\.php$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> <add input="{QUERY_STRING}" pattern="^slug=([^=&amp;]+)$" /> </conditions> <action type="Redirect" url="{C:1}" appendQueryString="false" /> </rule> <rule name="Rule6" stopProcessing="true"> <match url="^([^/]+)/?$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php?slug={R:1}" /> </rule> <rule name="Rule7" stopProcessing="true"> <match url="^index\.php$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> <add input="{QUERY_STRING}" pattern="^slug=([^=&amp;]+)&amp;details=([^=&amp;]+)$" /> </conditions> <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" /> </rule> <rule name="Rule8" stopProcessing="true"> <match url="^([^/]+)/([^/]+)/?$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php?slug={R:1}&amp;details={R:2}" /> </rule> 

还有我的试验来转换它们:

 #Rule1 RewriteCond %{REQUEST_METHOD} !POST RewriteRule ^translation\.php(.*) /translation? [R=302,L] #Rule2 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^translation(.*) /translation.php [R=302,L] #Rule3 RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{QUERY_STRING} ^lang=&$ RewriteRule ^switch\.php(.*) /switch/%1 [R=302,L,B] #Rule4 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^zone-client/index(.*) /zone-client/index.php [R=302,L] #Rule5 RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{QUERY_STRING} ^slug=&$ RewriteRule ^index\.php(.*) /%1 [R=302,L,B] #Rule6 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)/?$ /index.php?slug=$1 [R=302,L] #Rule7 RewriteCond %{REQUEST_METHOD} !POST RewriteCond %{QUERY_STRING} ^^slug=&;details=&$ RewriteRule ^index\.php(.*) /%1/%2 [R=302,L,B] #Rule8 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)/(.*)/?$ /index.php?slug=$1&details=$2 [R=302,L] 

请帮忙!

您需要执行几个步骤来重写LAMP堆栈中的重写。

  1. 确保已安装并启用了Web服务器的Mod_rewrite。

在基于Apache 2的服务器上,运行:

 sudo a2enmod rewrite 

这将激活模块或提醒您模块已经启用。 要使这些更改生效,请重新启动Apache。

 sudo systemctl restart apache2 

mod_rewrite现在完全启用。

现在你可以设置一个.htaccess文件来定义redirect的重写规则。

  1. 确保在你的.htaccess中使用RewriteEngine On来启用mod_rewrite,并且它出现在你的条件和规则之上。 有关在Apache中重写规则的一般信息,请参见https://httpd.apache.org/docs/2.4/rewrite/remapping.html

在你特定的用例中(从使用IIS URL Rewrite扩展生成的规则切换到直接的Apache重写),你可能想看看堆栈溢出中的一些以前的post,例如:

如果你想重新检查/testing和debugging你的正则expression式, 使用一个工具,如https://regex101.com/

  1. 添加你的条件和规则,一定要在每个规则上指明必要的标志。 (通常情况下,不适用于NC的区分大小写)。 请参阅https://httpd.apache.org/docs/current/rewrite/flags.html上的Apache的RewriteRule标志文&#x6863;

  2. testing您更新的.htaccess重写规则。 警惕 – .htaccess中的一个错字可能会导致500错误。