Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

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:
http://www.mysite.com/xmlfiles/detail-xml/246

to:
http://www.mysite.com/xmlfiles/detail-xml/246.xml

thanks,
brian
0
befidled
Asked:
befidled
  • 6
  • 3
  • 3
1 Solution
 
m_walkerCommented:
Are you saying that currently some one would go to http://www.mysite.com/xmlfiles/detail-xml/246 and your code will run and create the xml and send it back, and you want them to go to http://www.mysite.com/xmlfiles/detail-xml/246.xml 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
0
 
befidledAuthor Commented:
Here is the current url:

http://www.aeronautical.com/rfq/testxml/246/

If I add add a .xml however it just redirects to the homepage.
0
 
m_walkerCommented:
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.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
befidledAuthor Commented:
Here is what my current .htaccess file looks like:

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

# 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"
</FilesMatch>

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"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_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"
</FilesMatch>

Open in new window

0
 
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:

http://www.aeronautical.com/rfq/index.php?template=testxml&entry_id=246

0
 
m_walkerCommented:
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.
0
 
befidledAuthor Commented:
actually its reversed, the htaccess I posted rewrites the url with variable strings (i.e. http://www.aeronautical.com/rfq/index.php?template=testxml&entry_id=246) into the SEO friendly url (i.e. http://www.mysite.com/xmlfiles/detail-xml/246).

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

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

Open in new window

0
 
befidledAuthor Commented:
Won't that rename every URL on my site with a .xml suffix?
0
 
farzanjCommented:
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

0
 
befidledAuthor Commented:
That's good. I used a modified version of that solution.

thanks.
brian
0
 
farzanjCommented:
You are most welcome
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now