I have laravel running on an iis server. There is a default rewrite rule that redirects all unknown urls to index.php
I would like to apply this rule only to urls with a path starting with /api
and additionally to the exact path /login
So
mydomain1.com/assets/logo.png should point to the file or fail if it does not exist
mydomain1.com/api/users
mydomain2.com/api/posts/tags
mydomain1.com/login
should all redirect to index.php
This is the rule without the conditions:
<rule name="Laravel5" stopProcessing="true" enabled="true">
<match url="^" ignoreCase="false"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true"/>
</rule>
Related
I am using 3 domain-agnostic rewrite rules in my IIS. First i redirect from HTTP to HTTPS, then i redirect from naked-domain to www and in the end i remove .php from the URL by rewrite.
However, i see that whenever i access a page which is a subfolder and i access the index page in it, a trailing slash is added in the end of the url. Example:
Root -> subfolder called Items with an index.php in it
URL - www.xxx.net/items/
If i access a file other than index.php inside that folder it doesn't have a trailing slash
Root -> subfolder called Items with 2.php in it
URL - www.xxx.net/items/2
Normally, i wouldn't care but i noticed that google and the crawlers consider www.xxx.net/items and www.xxx.net/items/ as different entities which causes issues.
I know it's something easy but can't pinpoint it right now. Can someone show me how to get rid of this behaviour?
Here are the rules:
<rewrite>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Redirect naked domain to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Remove PHP" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rules>
</rewrite>
After reading a lot about it, it turns out that this is very expected behaviour and you need to factor it in. Basically when you know that you going to get a trailing slash, make sure that your links pointing to this page also include trailing slash in the end so you don't get a redirect.
I have the below config for url rewrite in my IIS
<rewrite>
<rules>
<rule name="Rewrite CI Index">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html|csv|ttf|woff|woff2|pdf|mp4|mov|ics" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(php_scripts)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/documents/intranet_documents/" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
</rules>
</rewrite>
The negate directive is there to prevent rewrite for website assets files (css, fonts, etc.) as well as other files from the documents upload/download section.
The issue I just came across is this, one of the urls is like this http://intranet/HR/Training-Basics however when I try to navigate to this url I get 404 because it is being caught by the negate function for file extension ics.
So, I need to be able to access this url, but at the same time I need to be able to download the "ics" files. What is the best way to do it?
You could modify the condition pattern from ics to\.ics, so that the rewrite rule will only rewrite based on file extension instead of specific string segment.
<rule name="Rewrite CI Index">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="\.(css|js|jpg|jpeg|png|gif|ico|htm|html|csv|ttf|woff|woff2|pdf|mp4|mov|ics)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(php_scripts)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/documents/intranet_documents/" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
I am trying to convert this web.config file to a .htaccess but cannot seem to get it correct. Can someone help?
<rewrite>
<rules>
<rule name="Main Rule" 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.php"/></rule>
<rule name="WordPress: http://valiantstag.thetribestaging.com" 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>
</rules>
</rewrite>
It looks like the default htaccess/web.config. I you go to options > permalinks, then change the structure, it should force update the .htaccess file whenever the site is running on apache.
When .htaccess doesn't have write permissions, wordpress will ask you to manually update it's contents.
We have an IIS 8.5 setup where a single website is bound to domain.com and contains a number of IIS applications accessed as domain.com/app1, domain.com/app2 etc.
Each of these applications points to the same physical path, so they all share a web.config. This is a specific CMS configuration.
I have applied the usual URL Rewrite rules (redirect to HTTPS, enforce lowercase, add trailing slash etc.) to the web.config each application shares but have realised these rules are only applied to the URL after the application name. The rules I have are just standard rules added using the URL Rewrite GUI:
<rewrite>
<rules>
<rule name="Enforce lowercase" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
</rule>
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
So, for example, http://domain.com/APP1/PATH redirects to https://domain.com/APP1/path/. Also, https://domain.com/app1 doesn't redirect to https://domain.com/app1/.
The HTTPS rule is fine, but can anyone tell me how I can configure the other 2 rules so that they work with the entire URL, bearing in mind that the specific application name (app1, app2 etc) needs to be handled generically.
UPDATE
I've discovered that I can enforce lowercase URLs using a global rule in IIS (at server level) which is sufficient for my needs. But it doesn't seem to be possible to replicate the website-level rule for adding/removing trailing slash.
You just need to change action URL.
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}/" redirectType="Permanent" />
</rule>
My site currently handles a URL encoded URL as one of the URI segments:
https://report-uri.io/home/pkp_analyse/https%3A%2F%2Fscotthelme.co.uk
I'm trying to replicate this functionality in an Azure Web App that's running IIS/8. I already have one rewrite rule for my MVC:
<rule name="CodeIgniter rewrite" stopProcessing="false">
<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/{R:1}" appendQueryString="false" />
</rule>
When I try to access the URL above I get a 500 error that states the following config file is not available:
D:\home\site\wwwroot\home\pkp_analyse\https:\web.config
What would be the correct way to handle the rewrite for this?