convert web.config rewrite rule to htaccess rewrite rule (All files without an extension to be processed as .cfm) - apache

I am moving a site from IIS 7 to Apache 2.4 and have the following web.config rewrite rule I am having trouble converting to .htaccess. The rule essentially allows for clean (seo friendly) urls by rewriting all files without an extension with the .cfm extension (e.g. www.mydomain/bag rewrites on the server as www.mydomain.com/bag.cfm. The working rule in web.config is shown below
<rule name="Rewrite all non extension requests to .cfm" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.cfm" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.cfm" />
</rule>
I have tried all the like posts I could find on here and unfortunately none of them worked for me. I am running Lucee 5.0 on CentOS 7 (Apache 2.4) if that matters. Any guidance would be greatly appreciated.

I don't really know about Tomcat; I assume you need Apache because Tomcat's no good for serving your non-script content.
You could set them up so that they both have the same document root, thus have congruent URLs, but have Tomcat listening on a different port, and not have that port open externally. Then you could proxy requests that are for (hidden) cfm files like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.cfm -f
RewriteCond %{THE_REQUEST} ^\S++\s++([^?\s]++)(\?\S*)?
RewriteRule ^ http://127.0.0.1:8080%1.cfm%2 [NS,NE,P]
RewriteRule (?<=.cfm)$ http://127.0.0.1:8080/404.cfm [NS,NE,P]
The second rule is an example to pretend that the cfm files are not there if request directly.
You will need the appropriate proxy module(s) enabled.

Related

Unable to convert IIS web.config URL Friendly to .htaccess mod_rewrite

I'm migrating a web application from a Windows (IIS) server to a Linux (Apache) and I have been a few days trying to adapt the application, configs, etc. for Linux/Apache.
Everyting is working except the url friendly I was using with Windows/IIS. And I don't know if this is actually a URL Friendly, a simple redirect or anything else.
This is what I want:
I have a 'app.php' file which is like the main application 'index', and shows different 'pages' depending on the old/ugly url parameters:
https://example.com/app.php?qpage=5&qsubpage=2&action=new
Instead of writting this, I want to use friendly urls like this:
https://example.com/section5/?qsubpage=2&action=new
(Please notice that the second and third parameters 'qsubpage' and 'action' are optional!)
Ok, with IIS and this lines in web.config, it works fine:
<rewrite>
<rules>
<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="app.php?qpage={R:1}" />
</rule>
</rules>
</rewrite>
Then, in 'app.php' file I get the current URI and (optional) parameters to show one content or another.
However, I can't reach to get it working with .htaccess.
This is what I have know. It works fine only if there is just one parameter:
If I go to...
https://example.com/section5
...it shows the correct 'page' correctly.
However, if I go to...
https://example.com/section5/?qsubpage=2
or
https://example.com/section5?qsubpage=2
...'qsubpage' is ignored.
If I go to
https://example.com/section5&qsubpage=2
...I get a '404: url not found' error (however it works in Windows)
This is my .htaccess rule:
RewriteEngine On
RewriteRule ^([A-Z-a-z-0-9]+)/?$ simplygest.php?qpage=$1&qsubpage=$2 [L]
I have tried some online mod_rewrite generators and even web.config to .htaccess converters online with no success.
Thanks!

Converting a web.config rewrite rule to Apache format

I would like to convert this web.config
<rule name="cambiarPass" stopProcessing="true">
<match url="^cambiarPass/" />
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
</rule>
to .htaccess. Please help!
Bear in mind that I do not use IIS but the provided code seems pretty self-explanatory in terms of how it should be converted to an Apache rewrite rule.
The <match url="^cambiarPass/" /> line is set to apply URL rewriting only to URLs (paths) which begin with cambiarPass/. The
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
line is the one doing the rewriting and redirecting all matched URLs to modulos/cambiarPass/controller.php. The appendQueryString attribute is obviously a synonym for the Apache QSA rewrite flag meaning the rewrite process will discard and ignore any existing query string data during the rewrite. The stopProcessing attribute seems to be yet another equivalent for the Apache L rewrite flag meaning if this rule is matched any additional rewrite rules that might follow the current rule will simply be ignored.
Here is the complete code.
RewriteEngine on
RewriteRule ^cambiarPass/ modulos/cambiarPass/controller.php [L]

Converting web.confg into .htaccess

I am trying to convert from using web.config on IIS to .htaccess on apache.
Although, i've got everything working, but got stuck on one thing.
<rule name="Imported Rule 2">
<match url="^blog/([a-zA-Z0-9_-]+)(|/)$" ignoreCase="false" />
<action type="Rewrite" url="index.php?url=blog&id={R:1}" appendQueryString="false" />
</rule>
I am trying to convert the above code into .htaccess.
If anybody could help, it would be gratefully appreciated.
Try to add this config to .htaccess
RewriteEngine on
RewriteRule ^blog/([a-zA-Z0-9_-]+)(|/)$ index.php?url=blog&id=$1

how to convert from IIS to apache httpd rewrite rule

I saw some examples on how to convert IIS to htaccss.
I'm using httpd conf (for performance reasons) + i used online converter for IIS --> htaccess.
when converting this rule:
<rule name="videorecords URL" patternSyntax="Wildcard">
<match url="videorecords/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="videorecords/{R:1}" />
</rule>
to:
#RULE VIDEORECORDS URL
RewriteRule videorecords/* videorecords/$1 []
the apache doesnt load and i see in the logs:
Syntax error on line 211 of httpd.conf: RewriteRule: unknown flag ''
Your flags are blank: []. So you need to either add a flag in ther (like L) or remove the square brackets.
Though, I don't think that the rewrite rule online converter gave you is actually what you want.

IIS Rewrite does not work for the root of an application

With IIS7, URL Rewrite 2 there is an MVC4 application APP on server SRV. The following rewrite should happen.
http://SRV/APP into http://SRV/APP/
I tried by creating the AddTrailingSlash rule. However, it does not work for the application's root. It does work for directories under the root, so the following rewrite is done
http://SRV/APP/pipapo into http://SRV/APP/pipapo/
What has to be done so the rewrite also works for the root?
The following rule seems to work for me:
<!--Add trailing slash to root non-file url-->
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<!--Match uri's missing trailing slash-->
<add input="{PATH_INFO}" pattern="(.*[^/])$" />
<!--Ignore any uri containing a period (probably a better way to do this but IsFile was not behaving as expected for me)-->
<add input="{PATH_INFO}" pattern="(.*?)\.(.*?)" negate="true"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="{PATH_INFO}/" />
</rule>
By default, the built in add trailing slash does not apply to directories or filenames... If you want it to apply to directories (like in the above example http(s)://srv/app), you have to modify the rule and delete the Condition that has "Type: Is Not a Directory". Don't forget to apply...
Happy URL Rewriting! :)