Can I use htaccess to add suffix to URL string?

I have a page on my site that renders XML. I'd like it if the url string would end in .xml. Is it possible to make this happen using htaccess?

For instance I want to change:


Who is Participating?
farzanjConnect With a Mentor Commented:
Yes, it would.  Sorry so should it contain xml keyword in the URL

Ok.  Does this look like what you need.  Here I am assuming that you need to put xml at the end if URL contains xmlfiles in it.

If it doesn't work, try removing the last \
RewriteRule ^(.*)/xmlfiles/(.*)$ $1/xmlfiles/$2\.xml [L,R=301]

Open in new window

Are you saying that currently some one would go to and your code will run and create the xml and send it back, and you want them to go to to have the xml rendered and sent back?
ie: having the xml extension be treated as a script (ie: 246.xml is the script name to create the script)

if so something like this might work...

AddHandler cgi-script .xml
Options +ExecCGI
befidledAuthor Commented:
Here is the current url:

If I add add a .xml however it just redirects to the homepage.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

how does the page get created now?

It looks like the 246 is a folder, and in that folder is a "default" script that creates the xml.  
I guess what I am trying to workout is how the script that creates the xml gets called from the web link.  
befidledAuthor Commented:
Here is what my current .htaccess file looks like:

# secure .htaccess file
<Files .htaccess>
 order allow,deny
 deny from all

# Dont list files in index pages
IndexIgnore *

# EE 404 page for missing pages
ErrorDocument 404 /index.php?/

# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp)$">
  ErrorDocument 404 "File Not Found"

RewriteEngine On

RewriteBase /

# remove the www
RewriteCond %{HTTP_HOST} ^(www\.$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Remove index.php
# Uses the "include method"
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(corporate|search|home|_includes|data|systems|services|news|events|platform|members|promos|rfq||members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

# Remove IE image toolbar
<FilesMatch "\.(html|htm|php)$">
  Header set imagetoolbar "no"

Open in new window

befidledAuthor Commented:
I'm using ExpressionEngine as my CMS. In this case the 246 is the entry number and the htaccess file above rewrites the url string so that it looks like a folder. Without the rewrite it would probably look something (not totally sure though) like:

First up, I would like to point out that I've never done this before, so tyring to work it out as I go.

The way I see it.

The htaccess you posted rewrites the "nice url" you want to the actual url to call index.php?doc id
When you add the .xml it does not work.

So I think what is needed here is to drop the .xml from the end with something like

#if it has a .xml drop it.
RewriteRule ^(.*)\.xml$ $1/ [L,R=301]

The way I think it reads is
^ Start of the url
.* any number of characters in () do store as $1

\. (esc the dot)
xml the extension.

I'm not sure if the [L,R=301] is needed or not.
If you put that line above the remove www, then it should drop the xml and then process as normal.
befidledAuthor Commented:
actually its reversed, the htaccess I posted rewrites the url with variable strings (i.e. into the SEO friendly url (i.e.

I need it to allow me to add the .xml, not drop it.

Please try this
RewriteRule ^(.*)$ $1\.xml [L,R=301]

Open in new window

befidledAuthor Commented:
Won't that rename every URL on my site with a .xml suffix?
befidledAuthor Commented:
That's good. I used a modified version of that solution.

You are most welcome
All Courses

From novice to tech pro — start learning today.