How to remove .php extension on Azure PHP Web App - azure

How do I remove the .php extension on a Azure PHP Web App ?
using at htaccess file with the following, does NOT work
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Ideas ?

Azure Web Apps use IIS to host your application. The best equivalent of Apache's .htaccess system is actually using web.config file to handle URL rewrite in IIS.
For example, you can just put the following web.config file into your web app's root folder to hide .php extension.
Referece:
https://www.saotn.org/iis-url-rewrite-hide-php-extension.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="hide .php extension" 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="{R:1}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Hope it helps.

Related

Convert Apache .htaccess rules to IIS web.config to append .php file extensions

I am using below .htaccess code. But I need to convert my .htaccess file content to web.config file. I have no idea how to do it. please check my .htaccess code below:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
How can I convert this to web.config?
we can put below content to the web.config file
<?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="/{R:1}.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
<system.web>
<compilation tempDirectory="D:\WWW\ovintours.com\tmp" />
</system.web>
</configuration>

Redirect from non-www to www using 301 on IIS

I have hosted my website on a windows server on 1&1.
There, is not possible add on the web.config the the elements <rewrite><rules>... So I am using an .htaccess file...
I am not so expert, I expected it would work just on Apache server. I was wrong! My .hataccess works also on IIS.
Here the code:
//Rewrite to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
The problem the redirect is done using a 302. I want a 301 redirect. I am using fiddler and this is the result:
The website where I have the problem is www.renovahaus.it
How can i solve my problem?
Thanx
This is normally done with the URLRewrite module, in the case of 1&1 this is disabled in the web.config however when you use a .htaccess file the contents of the .htaccess are still translated to the underlying web.config.
You can read a great tutorial on the conversion process here
But the summary is you are probably looking for the code block :-
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Which should transparently add the following to the web.config file (Even though you can't edit it directly)
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</rule>
<rule name="Imported Rule 2" 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?q={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

How to convert htaccess rules to web.config for IIS7/Symfony?

I have a symfony2 project developed on a linux server and have migrated it (unfortunately!) to a windows server for reasons outside of my control. This all works as it should except for the url rewriting. I tried using the IIS URL Rewrite Module but it failed when converting most of the rules (and stupidly i didn't save a list of which ones it failed on).
It mostly works fine but the app.php is still present at the start of all urls which it shouldn't be. So the urls are domain.com/app.php/correct/path when they should be domain.com/correct/path
Unfortunately i rarely use windows servers and am not good at the web.config syntax so if anyone could suggest what is missing to remove the app.php from the start of all urls on the windows server then that would be greatly appreciated!
The original .htaccess file is:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
The converted web.config file is currently:
<?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="IsFile" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Thanks very much!
Dave
Have just had this same issue while setting up symfony2 on a Windows server using IIS7.5.
The trick is to go into you web site management section. Look for 'url rewrite', look for 'Import Rules'(somewhere on the right). Then on the new dialogue browse for your .htaccess file in 'SymfonyProject/web/' by default. IIS will throw an error for the trailing slash rule so delete that (Everything seems to work fine without it) and press apply. Viola no app.php.
Be sure to add app.php to your list of default pages also.
I did the above using the standard htaccess file shipped with Symfony2.
EDIT As Requested Below
In the Import Dialog I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / <-This line needs deleting as IIS states "This directive was not converted because it is not supported by IIS: RewriteBase "
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
So the begining of my web.config looks like this:
<defaultDocument enabled="true">
<files>
<add value="app.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="app.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Also I have set my web directory as my root directory.
Hope this helps.
Doug.
I know this is an old question but today i had the same problem and i searched everywhere and took me a lot of time to fix it by replace the .htaccess file in web directory to web.config file with this content hope this will help some one .
<?xml version="1.0"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="app.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Symfony Front Controller" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="app.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I had same issue on a Plesk Linux to Plesk Windows migration. I removed the htaccess file and edited the default web.config file according to previous comments.
In my case, I just needed to change the "app.php" with "index.php" and then it works. This is the content of my web.config file
Hope it will helps !
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Symfony Rewrite" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<tracing>
<traceFailedRequests>
<clear />
</traceFailedRequests>
</tracing>
</system.webServer>
</configuration>

Mod_rewrite in Azure

I was write simple .htaccess and test it on Windows Azure Web Sites, but mod_rewrite didn't work there. Why? How I can reconfigurate my Azure?
RewriteEngine on
AddDefaultCharset utf-8
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test.html$ test.php?%{QUERY_STRING} [L]
.htaccess file is not recognizible by Azure Web Sites.
Azure Web Sites run on Microsoft IIS.
IIS has a URL Rewrite module, very similar to mod_rewrite for Apache. You can configure URL Rewrite rules by having a web.config file in your site root folder.
Follow the Creating Rewrite Rules article and scroll down to the "View the rule in Config file" to get an idea what it looks like.
Your rules defined, will look like this in web.config (and will most probably work as expected):
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^test.html$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="test.php?{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>

IIS Rule Convert To .htaccess Rule

I need some help writing a .htaccess rule that:
Removed the .php extension from a file name
Rewrites variables.
So, a url like www.example.com/contact-us/456/789 would be interpereted by the server as www.example.com/contact-us.php?a=123&b=456&c=789
The following IIS web.config rule works a treat (using a rewrite map):
<rule name="Dynamic Rewriting" stopProcessing="true">
<match url="^([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{DynamicRewriting:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/{C:1}.php?page={C:1}&a={R:2}&b={R:3}&c={R:4}" />
</rule>
<rewriteMap name="DynamicRewriting">
<add key="contact-us" value="contact-us" />
</rewriteMap>
Can anyone show me the .htaccess rule I need?
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^/?([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$ /$1.php?page=$1&a=$2&b=$3&c=$4 [L,QSA]

Resources