How to rewrite the index.php of Codeigniter on Windows Azure - codeigniter

How to remove index.php in codeigniter on Windows Azure and IIS?
Can I rewrite the URL for the index.php of Codeigniter without a particular module?

You can add rewrite rules in your web.config file. Add the following to the system.webServer section:
<rewrite>
<rules>
<rule name="Rule" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

Create a file name web.config in wwwroot
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="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?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

You can also implement other rewrite rules like
<rule name="a rule">
<match url="^xxx/(.*)/(.*)-(.*)\.xxx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="controller/method/{R:3}" />
</rule>
with one condition, is to change $config['url_protocal']='PATH_INFO'; in config/config.php this will tell URL rewrite module to use re-writed URI instead of original URL, otherwise you will have 404 page not found problem.

Related

Angular 5 rewrite rule when using DNS shortName for URL ( IIS 8.5)

I am using an shortname ( DNS entry) to map my angular 5 app URL.
Application is hosted in IIS as shown in the below image,
I have mapped the path to application to a short-name called "arcm".
So the moment, I type this short-name in browser address bar, I see my default route.
But the problem is,
if I refresh it on any route, I see 404 error.
Following is my web.config file and rewrite rule,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="AngularJS" stopProcessing="true">
<match url="arcm/" />
<action type="Rewrite" url="http://arcm/" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
the following changes resolve my issue,
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Http Redirection" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP}" pattern="off" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
<rule name="AngularJS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" negate="true" pattern="\.axd$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

iis url rewrite user-friendly urls multiple rules conflict and not wokrs

I've been trying to create user-friendly urls but multiple rules seems to conflict.
I need to create it like:
www.example.com/destination/abc
www.example.com/river/cde
With this code:
<rules>
<rule name="RedirectUserFriendlyURL3" stopProcessing="true">
<match url="^destination\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" 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="destination.php?{R:1}={R:2}" />
</rule>
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^river\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" 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="river.php?{R:1}={R:2}" />
</rule>
</rules>
The rules above do not work together and both pages redirect to the one that's set in the first rule. So I get the error that $var is undefined and get no result on page, but the url is different.
I get this result I need
www.example.com/destination/abc
www.example.com/river/cde
But somehow it seems to redirect just in 1 rule.
Please any feedback, I checked many same posts but could find a solution
If your query is same with your php filename e.g
river.php?river=abc and destination.php?destination=abc
then you can change your code like this:
<action type="Rewrite" url="{R:1}.php?{R:1}={R:2}" />
So your filename should be same with query_string in url to work

URL Rewrite Rule to remove www. for all URLs except for 1

I have a working rule that removes www. for all urls coming in. I have a webserivce that the redirect will change from a POST to a GET. I need to exclude the URL https://www.foo.net/WebServices/service.asmx from this rule.
I have looked at other solutions on SO with no success.
<rewrite>
<rules>
<rule name="Remove WWW" enabled="true" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(foo\.net)" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" />
</rule>
</rules>
</rewrite>
You can do that easily by adding a condition to "not match" (aka adding "negate='true'" ), something like this should work:
<rules>
<rule name="Remove WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.foo\.net$" />
<add input="{URL}" pattern="^/WebServices/service\.asmx$" negate="true" />
</conditions>
<action type="Redirect" url="https://foo.net/{R:1}" />
</rule>
</rules>

IIS Rewrite Rule how to check requesturl + .php file is exists or not in rule

i want to check that file with requestedurl + .php is exists or not.
in apache i can do this with RewriteCond %{REQUEST_FILENAME}.php -f
so that it can rewrite my request test.com/login to test.com/login.php
i want to do same with iis rewrite rule.
i have tried with following.
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}\.php" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
<rule name="Imported Rule 2">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="allpages.php" />
</rule>
</rules>
</rewrite>
but it doesnt works.
thanks in advance for help
Change {REQUEST_FILENAME}\.php to {REQUEST_FILENAME}.php in first rule.
Further there is no need to check if requested URL exists as directory.
To rewrite 'test.com/login' to 'test.com/login.php' only if login.php exits, use:
<rule name="Rewrite to PHP">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
Tested

wildcard redirect with url rewrite by web.config

I'm using the url rewrite features in web.config to redirect my subdomains. Here is my rule :
<rule name="redirect_page">
<match url=".*" ignoreCase="false" negate="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
<add input="{HTTP_HOST}" pattern="^([\w\-]+)\.mySite\.com$" />
</conditions>
<action type="Rewrite" url="profile.aspx?name={C:1}" />
</rule>
This works great :
http://myUser.mySite.com redirect to http://myUser.mySite.com/profile.aspx?name=myUser
BUT
http://myUser.mySite.com/image/myImage.jpg redirect to http://myUser.mySite.com/profile.aspx?name=myUser
=> What I want :
http://myUser.mySite.com/image/myImage.jpg redirect to http://myUser.mySite.com/image/myImage.jpg
Any idea ?
Well this is a little tricky but here is the solution :
<rule name="SubDomainDoNothing" stopProcessing="true">
<match url="(.+)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(\w+)\.plugandtable\.com$" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>
<rule name="SubDomainRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(\w+)\.plugandtable\.com$" />
</conditions>
<action type="Rewrite" url="profile.aspx?name={C:1}" />
</rule>

Resources