3

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.

Dave S
  • 33
  • 2
  • 1
    You should be able to customize this answer to use .cfm instead of .html: http://stackoverflow.com/questions/5745490/rewrite-rule-to-add-html-extension – Jordan Nov 29 '16 at 21:22
  • Thank you, yes I have tried that without luck previously. I am thinking it may have something to do with the ".cfm" file being proxy'd through tomcat after Apache. When I use the link you suggest I seem to get close as when I turn on logging I see the following. `strip per-dir prefix: /home/domain/public_html/bag.cfm -> bag.cfm applying pattern '^(.*)$' to uri 'bag.cfm' pass through /home/domain/public_html/bag.cfm Negotiation: discovered file(s) matching request: /home/domain/public_html/bag (None could be negotiated).` – Dave S Dec 01 '16 at 15:29
  • By the time the htaccess is reached, it's too late to do a rewrite from non-.cfm to .cfm. – Kevin B Dec 02 '16 at 16:15
  • Where would the rewrite need to be then? – Dave S Dec 02 '16 at 20:51

1 Answers1

0

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.

Walf
  • 8,535
  • 2
  • 44
  • 59