Is it possible to call a Coldfusion cfc method from within .htaccess? - .htaccess

I'm calling a cfm file form htaccess to perform some manipulation on images like so:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
...
RewriteRule \.(jpg|jpeg|gif|png) services/adaptive-images.cfm
</IfModule>
Inside the cfm file I'm resizing the image.
Question:
Would this also be possible by calling a method inside a cfc file? Something like this:
RewriteRule \.(jpg|jpeg|gif|png) services/ai.cfc?method=resize
Is there a way to make this work?
Thanks for any info!

The question you're asking can be distilled down to a couple of components:
Can a CFC method be called via HTTP?
Can a rewrite rule make an HTTP request?
Obviously the answer to the second one is "yes". And - as you seem to already know - the answer to the first one is also "yes". You demonstrate the syntax for this in your RewriteRule.
The thing you need to consider here is that CF just receives requests from the web server to fulfil an HTTP request. CF doesn't know (or care) whether the request can straight through from the web server unadulterated, or whether it was rewritten by the rewrite module first.
So... if you're getting an error, it's something else. What you're trying to do from a web server / rewrite module / CF standpoint is entirely possible.
I suggest you have a look at your logs and see what's causing the 500 error. Indeed if you have robust exception handling enabled, you should be getting the cause of the error displayed on the screen.
What happens if you call the URL you're rewriting to directly via the browser address bar?

Related

How to redirect a URL in htacess with query string to the same URL but different query string

Because of removing several languages from a multi-language website, I need to 301 redirect pages that end with ?lang=da, ?lang=de, and ?lang=nl to the same URLs but ending in ?lang=en. It sounds like a common scenario, but I haven't found the right code yet to accomplish this or tried some code because the purpose of that code was either to redirect to one new URL or to replace the URL but not the query string, while I need to replace the query string and keep the URL.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule ^ %{REQUEST_URI}?lang=en [QSD,R=301,END]
For this to work the rewriting module obviously needs to be loaded into your http server and it has to be activated for your http host too.
It is a good idea to start out with a 302 redirection and to only change that into a 301 once you are convinced everything is set up as required. That way you prevent ugly caching effects...
You can implement such rules in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (".htaccess"), but that has performance disadvantages and you also need to enable the interpretation of such files first. Please see the documentation of the tool you are using to learn how to do that.

Ignore last part of URL with .htaccess

We have a FAQ page /faq (tab style) where every question should have its own 'ghost' url/page. So users could visit eg.
/faq/question-1
/faq/question-2
/faq/question-3
The problem is question-1, question-2, question-3 are not actual pages but just sections on /faq. For SEO, aesthetics and usability reasons we do not want to work with ?q= or #
I've searched and tried every .htaccess thread I came across but without result.
Is there a way we can show the page/faq when visiting /faq/question-1 and keep the url /faq/question-1 with mod_rewrite? (we cannot hardcode it because we do not know all future question slugs) So basically something that tells the browser: if the first url part is /faq/, just ignore everything that comes behind but keep the url.
Thanks
This is a trivial rewriting task and it is unclear why this should not work for you:
RewriteEngine on
RewriteRule ^/?faq/.+ /faq [END]
Since you claim that you "tried every .htaccess thread you came across" and this clearly works the question is: why not in your setup? But since you did not tell us anything about your setup we cannot really offer more help...
These are some general hints though which you should go through:
Where did you implement the rules you tried? In the http server's host configuration or in a distributed configuration file?
If you are using a distributed configuration file (".htaccess") then how did you make sure such files are interpreted by your http server and how did you test that?
Did you check your http server's error log file for hints?
Did you make sure that you are not actually looking at cached responses? So did you really test with a fresh anonymous browser window using a "deep reload"?
Since the CMS you are using requires own rewriting rules, where did you add those rules you tried? Remember: the order is important!

HTACCESS: Redirect all the requests to another page, in place of index, hiding arguments?

I expose here my issue, I hope to explain myself clearly and correctly. In case of any specification, please ask me.
What I need is to redirect all the request (except the one to index.php) to another .php file, without the necessity to specify an argument.
here an example of what I need to do:
http://www.example.com -> shows index.php
but:
http://www.example.com/songs/title-of-the-song
should call the page: songs.php?dir=title-of-the-song of course without showin to the user the string "songs.php?dir=title-of-the-song" but just the URL
http://www.example.com/songs/title-of-the-song
It's what occurs with Drupal, but i'm not using that CMS for my site.
You are probably looking for something like that:
RewriteEngine on
RewriteRule ^/songs/(.+)$ /songs.php?dir=$1 [L]
Note: this is the version for the http host configuration. For usage in .htaccess style files you have to adapt it slightly. But if you have access to the host configuration you should always prefer that over .htaccess style files. Those files require an additional module, are notoriously error prone, hard to troubleshoot and really slow the server down.

Redirect to url within another url using .htaccess

I have a problem, I need to redirect users to a URL within a url.
For example:
When the user visits the URL: api.example.com/redirect/www.google.com
I want the user to be redirected to: www.google.com
I've been trying for a while now, but no sucess.
You are probably looking for something like that:
Variant for the main http server configuration:
RewriteEngine on
RewriteRule ^/redirect/(.+)$ http://$1 [L,R=301]
Variant for a .htaccess style file:
RewriteEngine on
RewriteRule ^redirect/(.+)$ http://$1 [L,R=301]
It obviously requires the rewrite module being available inside your local http server.
In general you should prefer putting such rules into the main server configuration, if you have access to that. A .htaccess style file should only be used if you have no other alternative. The reason is that such files are notoriously error prone, hard to debug and really slow the http server down.
A more robust approach would be to take the captured part of the url and hand it over to a script on the server side as a parameter. The script can validate the part, whether it is a valid "url" (to keep things simple here...). And maybe make a head request to check of the target exists and is usable. Only then it redirects. Otherwise it returns a meaningful error message.

redirectmatch changes post to get

I have two separated vhost. On the www. one, I have instaled this only line on .htaccess
redirectMatch 301 ^(.*)$ http://d_blur_blur_s.com$1
All works as expected except that in the case where the POST is converted to GET.
Please notice that the post has parametres as a get (i didn't do it, and I won't change it) i am just trying to avoid duplicated content. I am showing firebug trace.
I expect to have the POST redirected on the main domain with redirectmatch or other trick.
Update
I have half of the internal links on the site written with www and the other half without it. So I need to keep both public but not duplicated. I need the GETs forwarded as GETs and the POSTs frowarded as POSTs. the problem is big, I have 12000 pages indexed, and lot of forms. So I am first searching for a generic solution without changing code. I full control de server.
Thanks a lot
A redirect response from your server for whatever request you made, regardless of it being POST or GET request, always results in your client making a GET request (unless you somehow enable it to NOT automatically follow redirects--sometimes I do this with curl, but I don't know any widely used browsers with this functionality available easily to the user). The client browser takes the URL provided by the redirect and treats it as a URL to perform a GET request. No way around it, the POST data is discarded by the server since it was intended for the server resource at the original URL.
If you redirect outside of .htaccess, say in a PHP file to construct redirect response, your only option is to convert the POST parameters into a string of GET parameters and add it to the end of the URL you send back to the client with your redirect response.
I'm fairly confident there is not a way to do automatic POST parameter appending to redirect in the .htaccess file or in the httpd.conf files whether you use the redirect directive or the rewrite directive via the mod_rewrite module.
You redirect using 307 instead of 301 to keep the post data, but some browsers show a dialog to the user if he is sure he want to sent the post data, so not real pretty.
But I'd rather go and fix the problem at the root. The only way you would get a post to the wrong domain is if the html-form refers to the wrong domain (e.g. <form action="www.d_blur_blur_s/public/main/loginGenerator.php" ...). Just fix the html.
Don't worry about duplicated content as search engines never send post-requests, only get. You current solution should do the trick to prevent duplicated content.
The only way to do a redirect and preserve POST data (that I am aware of) is to use mod_rewrite with mod_proxy and then use P flag in RewriteRule.
On your www host enable mod_rewrite, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^ http://d_blur_blur_s.com%{REQUEST_URI} [P]
I bumped into this POST->GET rewrite as well. An API call like this:
curl -X POST https://example.com/api/user/123456789
was entering my API framework with a GET request method.
The cause seems to be related to POST requests with an empty body. To avoid this issue you can set the Content-Length header:
curl -X POST https://example.com/api/user/123456789 -H 'Content-Length: 0'
or pass something in the body:
curl -X POST https://example.com/api/user/123456789 -d ''
In HTTP 1.1, statusCode (307) indicates that the request should be repeated using the same method and post data. Change the redirect type to fix it.
Adding www to the base URL did it for me.

Resources