web.config到mod_rewrite返回400 Bad Request

时间:2014-07-17 08:23:07

标签: apache .htaccess mod-rewrite iis web-config

第一次在这里使用mod_rewrite我有这个规则在IIS上工作:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="services" stopProcessing="true">
                    <match url="^([a-z]+)/service/([a-zA-Z-]+)" />
                    <action type="Rewrite" url="serveis.php?id={R:2}&amp;lang={R:1}"/>
                </rule>
                <rule name="categories" stopProcessing="true">
                    <match url="^([a-z]+)/category/([a-zA-Z-]+)" />
                    <action type="Rewrite" url="subhome.php?id={R:2}&amp;lang={R:1}"/>
                </rule>
                <rule name="index_idioma" stopProcessing="true">
                    <match url="^([a-z]+)/index" />
                    <action type="Rewrite" url="index.php?lang={R:1}"/>
                </rule>
                 <rule name="index" stopProcessing="true">
                    <match url="http://www.domain.com" />
                    <action type="Rewrite" url="http://www.domain.com/index.php?lang=en"/>
                </rule>
                  <rule name="kinds" stopProcessing="true">
                    <match url="^([a-z]+)/kind" />
                    <action type="Rewrite" url="tipusserveis.php?lang={R:1}"/>
                </rule>
                <rule name="company" stopProcessing="true">
                    <match url="^([a-z]+)/our_company" />
                    <action type="Rewrite" url="/empresa.php?lang={R:1}" />
                </rule>
                 <rule name="contact" stopProcessing="true">
                    <match url="^([a-z]+)/contact" />
                    <action type="Rewrite" url="/contacto.php?lang={R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

现在我想使用apache的mod_rewrite来实现这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/([a-z]+) index.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/our_company/?$ empresa.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/contact/?$ contacto.php?lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/service/([A-Za-z0-9-]+)/?$ serveis.php?id=$2&lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/category/([A-Za-z0-9-]+)/?$ subhome.php?id=$2&lang=$1 [NC,L]
    RewriteRule ^/([a-z]+)/kind/([A-Za-z0-9-]+)/?$ tipusserveis.php?id=$2&lang=$1 [NC,L]
</IfModule>

但他们都没有工作。

有人可以在这帮助我,至少给我一些关于我应该如何做的指示?

编辑: Plesk Panel会自动将其添加到httpd.conf

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com$1 [L,R=301]
<IfModule>

我想它会从网址中删除www,但我不知道它是否也会停止应用其他规则。 感谢

2 个答案:

答案 0 :(得分:1)

由于您的URI模式中缺少锚点mod_rewrite^,您在$中收到错误。在根.htaccess或Apache config中尝试这些规则:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    RewriteRule ^/?([a-z]+)/?$ index.php?lang=$1 [NC,L,QSA]
    RewriteRule ^/?([a-z]+)/our_company/?$ empresa.php?lang=$1 [NC,L,QSA]
    RewriteRule ^/?([a-z]+)/contact/?$ contacto.php?lang=$1 [NC,L,QSA]
    RewriteRule ^/?([a-z]+)/service/([a-z0-9-]+)/?$ serveis.php?id=$2&lang=$1 [NC,L,QSA]
    RewriteRule ^/?([a-z]+)/category/([a-z0-9-]+)/?$ subhome.php?id=$2&lang=$1 [NC,L,QSA]
    RewriteRule ^/?([a-z]+)/kind/([a-z0-9-]+)/?$ tipusserveis.php?id=$2&lang=$1 [NC,L,QSA]
</IfModule>

答案 1 :(得分:1)

1)您的分配(第二个参数)不能是相对路径。它需要以/或以重定向开始,而不仅仅是例如“的index.php”。这是你的400s的直接原因 - 你的错误日志可能清楚地显示了一个不以/开头的URL路径(这是无效的)

2)你应该忽略将这些规则放在.htaccess中的建议。这使得mod_rewrite维护起来要复杂得多。

相关问题