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

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!

Related

IIS Rewrite very confusing

concerning IIS Rewrites.
I want to change this url on my local IIS Instance
http://localhost/MySite/health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
to redirect to this url:
http://localhost/MySite/Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
As you can see I don't want the incoming request to use
Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
instead to use
health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
The Health.svc is the WCF endpoint name, so I just want /health with the key and filter parameter at the end as shown.
Whatever I put in my web config rewrite it still doesn't work. I am rather confused what bit of the url to put in, as the regex seems to be valid as I can test it in IIS and online regex validators.
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="MySite\/health\?key=([0-9a-zA-Z=]+)&([a-z])" />
<action type="Rewrite" url="MySite/Health.svc/{R:1}/{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
How can I get this to work? I have got the rewrite module installed as can see it in IIS an also can see the dll is registered.
If you want to match the value of the query string in IIS, you need to use {QUERY_STRING}. Here is a demo:
<rewrite>
<rules>
<rule name="Test">
<match url="(Service1)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(key)=(.*)" />
</conditions>
<action type="Rewrite" url="Service1.svc/{C:2}" />
</rule>
</rules>
</rewrite>
This is my web.config.
This URL:
http://localhost/Service1?key=getdata
will redirect to this url:
http://localhost/Service1.svc/GetData
For your last question, why add the MySite prefix? This is because the URL in the Rewrite URL will be used as the redirect URL. Notice that it uses back-references to preserve and rearrange the original URL pieces captured during pattern match. For Rewrite, all prefixes other than localhost must be provided in the Rewrite URL.
I managed to get it working with going to all sorts of sites as its not obvious at all.
They key seemed to put the conditions in and then a {QUERY_STRING} with regex which can then create the {C:1} and {C:2} groups that are pushed into the new rewrite
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="^health" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="key=([0-9a-zA-Z=]+)&([a-z])" />
</conditions>
<action type="Rewrite" url="/MySite/Health.svc/{C:1}/{C:2}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
I found it confusing knowing what url to match but with a few simplified tests of just ^health I could see more easily and play around with getting the query string parameters. I had to provide the Rewrite with the /MySite/ prefix which is confusing as the match didn't need that!

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

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.

301 redirect on IIS. Redirect non-www to www

I ought to redirect my website from non-www to www on IIS 6. e.g if I enter domain.com in the url, it has to redirect to www.domain.com for SEO optimization.
I followed the video "http://www.youtube.com/watch?v=PYxabNrIMQ4" for creating my rewrite rule. And I made it as below
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
But it is not working as expected. Help me out to find the solution.
Note:
My Visual studio shows warning like "The element 'system.webServer' has invalid child element 'rewrite'".
I believe it doesn't make any problem.
According to this video you can redirect using the IIS Manager UI. Make sure you have a second site for the non-WWW version, with the same "Location" as the WWW version of your site; when creating it, be sure to specify your non-WWW domain for the "Host Header" field.
After creating it, right-click the site and go to Properties. Disable logging if you're so inclined (probably not needed because you'll be redirecting anyways), then go to the "Home Directory" tab. Now remove the Application by clicking the appropriate "Remove" button, since it's not used for redirection. Next, select the "A redirection to a URL" radio button, be sure to check "The exact URL entered above" and "A permanent redirection for this resource".
Lastly, in the "Redirect to" field, enter your full WWW url followed by $S$Q (i.e. http://www.example.com$S$Q) -- the $S$Q are important to capture any path and query string the client might send, so if the user goes to http://example.com/foo/bar.html they will be appropriately redirected to http://www.example.com/foo/bar.html instead of just http://www.example.com/ . Click Apply and OK, and your redirection should be set!
Does this works ? remember to add in the root web.config
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>

IIS URL Rewrite rule not being triggered

I am pretty sure that I have my WCF URL Rewrite Rule coded correctly in my Web.config file:
<rewrite>
<rules>
<rule name="RemoveSVC" stopProcessing="true">
<match url="MyWebSvc/([a-zA-Z]+)-svc/(.*)" />
<action type="Rewrite" url="MyWebSvc/{R:1}.svc/{R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
The rewrite rule seems to be working in the "Test pattern..." widget. For instance:
http://localhost/MyWebSvc/thinga-svc/majigs
gets correctly mapped to:
{R-1} thinga
{R-2} majigs
And I would like the URL to be re-written to:
http://localhost/MyWebSvc/thinga.svc/majigs
Seems simple enough, however, when I try to test the URL from a browser:
http://localhost/MyWebSvc/thinga.svc/majigs
works, but
http://localhost/MyWebSvc/thinga-svc/majigs
does not appear to be re-written, and I get a 404 response code. Do I have to configure the rule to be triggered with some other setting?

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! :)