My wordpress has 2 rules of URL rewrite, this is the 1st rule:
<rule name="wordpress" patternSyntax="Wildcard">
<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"/>
</rule>
This is the second rule:
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
They both works independently. But if I combine both rules like below:
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<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"/>
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Only the first rule is working, the 2nd rule is ignored as if it does not exists. How to make the 2nd rule works together with 1st rule?
You can try this rule:
<rule name="Redirect to https" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="wordpress" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url=" index.php" />
</rule>
Best regards,
Sam
Related
Hello i am trying to make it so when i put in url /edit then put me in a directory /edit. Everything else is only in the index.php
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(\.png|\.jpg|\.gif|\.jpeg|\.js|\.css|\.ico|\.mp4|captcha.php|servertime.php|robots.txt)$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
Currently this code is redirecting everything to an index.php
When I add:
<rule name="Imported Rule 1-1" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^edit/?(.*)$" />
<action type="Rewrite" url="edit/index.php" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php" />
</rule>
The directory /edit opens but I lose the other redirect all to index.php
You need a new "rule" just make sure you add it before the other rules :)
<rule name="rule1" stopProcessing="true">
<match url="^edit" />
<action type="None" />
</rule>
I know there been quite a few posts on this already but the answers do not work for me.
when I type in example.com in the url I want it to redirect to https://www.example.com
I have these rules but when I put in example.com I get 404.
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" ignoreCase="true" matchType="Pattern" negate="false" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
edit
I also tried this but still not getting expected re
According to your url rewrite rule I found you set a url rewrite rule at first. That means if your url match the (.*), it will rewrite the url to /index.html and will not go to any other url rewrite rule. So your "Redirect to www" and "Redirect to HTTPS" rules will not be fired.
To solve this issue, I suggest you could try to use below rules. It will fire the redirect rules firstly, then fired rewrite rule.
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" ignoreCase="true" matchType="Pattern" negate="false" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
I have these rules but when I put in example.com I get 404.
As far as I know, 404 means page not found, I suggest you could try to use fail request tracing to track the actually url IIS have redirect to.
Details, you could refer to below article:
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
I wanted to convert this configuration file into a htacess file.
I'm looking for a way to convert my rewrite rules to an .htaccess file. I couldn't find any tool to automatically do this and all I can get myself is 500 Internal Server Errors.
The web.config file looks like this:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)$" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
<rule name="RewriteUserFriendlyURL1" 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?module={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&page={R:2}" />
</rule>
<rule name="RedirectUserFriendlyURL3" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL3" 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?module={R:1}&p={R:2}&id={R:3}" />
</rule>
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}/{C:4}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&p={R:2}&id={R:3}&page={R:4}" />
</rule>
<rule name="RedirectUserFriendlyURL5" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&date=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/page/{R:3}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL4" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/page/{R:5}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL5" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?date=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" />
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.24:00:00" />
</staticContent>
</system.webServer>
</configuration>
So my solution was like this.
RewriteRule ^index\.php$ {C:1}
RewriteRule ^([^/]+)/?$ index.php?module=$1
RewriteRule ^index\.php$ {C:1}/{C:2}
RewriteRule ^([^/]+)/page/([^/]+)/?$ index.php?module=$1&page=$2
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?module=$1&p=$2&id=$3
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}/{C:4}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$ index.php?module=$1&p=$2&id=$3&page=$4
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
Bus I'm having some problems with this code. Some rules are really not working. Will you help me to solve this problem.
Thanks in advance!
first you should check mod_rewrite.c is active in your server or not , then turn it on and start re-write urls like this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?module=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?module=$1&p=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?module=$1&p=$2&id=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/page/([^/]*)$ /index.php?module=$1&p=$2&id=$3&page=$4 [L]
</IfModule>
Trying to set the redirect rule which would force redirect for everything with the exception of one sub domain and one web page:
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^www1\.mywebsite\.com$" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/file\.txt$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Rewrite" value="max-age=31536000; includeSubDomains; preload" />
</rule>
</outboundRules>
I have a site at domain.com. I've recently installed an SSL certificate so I can use HTTPS.
In IIS, I created bindings for domain.com and www.domain.com for http and https. Then I have a URL rewrite rule in my web.config that adjusts every url to point to https://domain.com
My issue is that when I go to http://domain.com or https://domain.com the site works, but http://wwww.domain.com and https://www.domain.com do not. Is this an issue with my rewrite rules or my binding?
Here is my web.config for reference:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
<rule name="Imported Rule 1">
<match url="^(register)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/register.php" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
i have solve this problem like this code block.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Temporary" />
</rule>
</rules>
</rewrite>