Apache URL rewrite change file extension from .htm to .cfm - apache

First let me apologize if this has been asked/answered before. I did a search and could not find what I was looking for. Some questions were close but just not enough. Again apologies for duplication.
Second, my work has always been with IIS and as such my apache knowledge is limited so again, my apologies for asking for a little extra "hand holding" as I learn how to address this issue. Much thanks to any and all for your help.
Now for my question...
I have http://www.mycompany.com/internalUser/[className]/beginClass.htm where [className] is dynamic, for example French101 or Biology210 but each always have beginClass.htm
What I need to do is have a URL rewrite that changes beginClass.htm to beginClass.cfm
What I am not sure of is where should this rewrite code go - in the httpd.conf or .htaccess file within /internalUser folder.
Furthermore, I have tried the following example adding it to the directory tag created for internalUser
-- Copied from httpd.conf
Alias /internalUser "c:/iUser/training"
<Directory "c:/iUser/training">
RewriteEngine on
RewriteBase "/iUser/training"
RewriteCond "$1.cfm" -f
RewriteCond "$1.htm" !-f
RewriteRule "^(.*).htm$" "$1.cfm"
</Directory>
Also created in .htaccess file and neither is working. When I attempt to open the .htm it does not redirect or open the .cfm version. The .htm file is still being opened.
Thanks in advance for any and all help.

Use below for rewrite,
RewriteEngine On
RewriteRule ^internalUser/([^\/]+)/beginClass.cfm$ internalUser/$1/beginClass.htm [L]

Related

htaccess doesn't work after moving from webhost to local Synology host

I have a hosted website where I use the following htaccess file for formatting of urls, These all work fine. The host uses Apache, but unfortunately doesn't show a version number. I think it's 2.4.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^item/([0-9]+)/(.*) /item.php?item=$1&title=$2 [L]
RewriteRule ^category/(.*) /showitems.php?category=$1 [L]
RewriteRule ^search/(.*) /searching.php?options=$1 [L]
RewriteRule ^searching/(.*) /showitems.php?search=$1 [L]
RewriteRule ^update/(.*) /showitems.php?update=$1 [L]
AddType application/x-httpd-lsphp .html .htm .shtml
I copied the entire site to my local Synology Diskstation with Apache 2.4.
The rewrite urls for category, search and update work fine. However, the urls for 'searching' and 'item' return 404 errors. 'Searching' is a header redirect from within 'searching.php'
Item is an oddity in the sense that it uses 2 get params in the result url. In trial and error mode I changed it to:
RewriteRule ^item/(.*) /item.php?item=$1 [L]
Which doesn't work either, however
RewriteRule ^itemitem/(.*) /item.php?item=$1 [L]
Works fine, which really puzzles me. This last rewrite also doesn't work when I add the second parameter again.
What am I missing? Or is there a better way to approach these rewrites in the first place that I could try?
What CBroe has commented on your post is properly the correct answer.
To be more specific and explain why then if your Apache 2.x server has MultiViews enabled it will try to match things up for you like directory names, file names on your behalf to make the user-experience easier.
However, this can in many cases confuse your rewrite rules that are expecting very specific regular expressions.
In most cases you can get away with just disabling MultiViews.
You disable it by adding to your Options in your .htaccess file.
-MultiViews
Please note that if the AllowOverrive directory does not allow this then your need to change the Apache configuration file for that vhost/directory to include the -MultiViews option.
You can read more about MultiViews here:
http://httpd.apache.org/docs/current/content-negotiation.html
The other thing and perhaps more correct way is to build your rewrite rules better and take advantage of things like the "!-f" or "!-d" parameter to let your rules know that you don't want to include files or directories and so forth.
A side note by the way on MultiViews, even though it's very fancy and neato - If you are running a production site please know that MultiViews create a hole lot of unwanted disk I/O and can slow down the Apache servers performance quite a bit! So it's always good practice to disable MultiViews.

mod_rewrite on 1and1 shared hosting

I'm trying to do some URL rewriting, but my host (1and1) and my little experience with .htaccess files give me troubles.
Specifically, I am trying to setup the .htaccess file on /news directory, where I have a show.php file. My purpose is to convert www.example.com/news/blahblah to www.example.com/news/show.php?article=blahblah.
My .htaccess file so far is (EDIT: updated my .htaccess, still no solution):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/news/([a-z0-9_-]+)$ /news/show.php?article=$1 [L,NC]
</IfModule>
I have messaged my host for details on how to enable the module, and they replied that I should input these lines to enable the module.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
I tried with RewriteBase, and adding the rest lines to the file, but I got no results so far. I am new to .htaccess coding, so I don't know much about these commands, and tutorials don't seem to answer my questions, so any help understanding what I'm doing wrong is dearly appreciated.
EDIT: I updated the .htaccess I have on the folder. I also uploaded the support's file on the root folder, but no result. I still get 404 Not Found errors.
The lines that you've added aren't required. It seems maybe a couple of things may be the issue. Either mod_rewrite isn't loaded, thus the <IfModule> block is never executed. Or the htaccess file is being ignored.
You should be able to tell if you remove the <IfModule mod_rewrite.c> and </IfModule> lines, then try going to a page. If you get a 500 Server error, that means mod_rewrite isn't loaded and the server is puking because the rewrite statements aren't recognized. Otherwise, you can try adding some jibberish into the htaccess file and if you get no errors at all, then it would seem the htaccess file is being ignored.
Seeing as you're on shared hosting you'll need to contact 1and1 support. I have no idea how they run their shared hosting, but if the module isn't loaded, then they need to load the rewrite module, something like:
LoadModule rewrite_module modules/mod_rewrite.so
otherwise they need to make your document root overridable:
AllowOverride All
so that the htaccess file can rewrite the URI.
EDIT:
got to mention that I have no .htaccess on the root folder, just the /news folder.
This is what the problem is then. Your rule needs to be changed to:
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]
Found the solution. I uploaded an .htaccess file at root folder containing only what support gave me, and also uploaded my own .htaccess file at the news folder, and it worked. The correct .htaccess of the news folder is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]
</IfModule>

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable 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 ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

htaccess Rewrite Rule for php to png stopped working

I have created several php files that dynamically build images for user rankings. I included an htaccess file in the image directory to translate the php files into png files so that the phpBB board would allow them to be used as ranks. Up until yesterday everything was working fine. I was using the following code in the htaccess file:
RewriteEngine On
RewriteRule ^rodeojones.png$ rodeojones.php
RewriteRule ^marine.png$ marine.php
RewriteRule ^treble.png$ treble.php
RewriteRule ^major.png$ major.php
RewriteRule ^hyghway.png$ hyghway.php
RewriteRule ^zypher.png$ zypher.php
etc... Now, all of the sudden, it isn't working. I am guessing this isn't the most efficient code and therefore it is no longer working for that reason, but I am not sure. This was my first adventure in php so I am lost as to a solution. Any help would be greatly appreciated. Thank you :)
Try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^(script)/([^.]*)\.php$ $1/image.png [L]
Hopefully will help you, though needs to find more specific to your scenario !

How do I add .gif to pathnames in Apache httpd?

This is a simple htaccess question for experts but I have been trying to get this sorted for a while. This was something a developer did before my time with this code. He truncated the image file extension from the requests. As an example,
/images/btn/Find a bear should get the URL changed internally to /images/btn/Find a bear.gif
All the images in the folder are .gif extensions.
I tried this URL rewrite at the root folder but it did not help.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^images/(.*)$ images/$1.gif
I know that RewriteRule is enabled on the server, etc. Please help.
You’re probably having an infinite loop. Try this:
RewriteCond $1 !.*\.gif$
RewriteRule ^images/(.*)$ images/$1.gif