Solved

Can I use htaccess to add suffix to URL string?

Posted on 2011-03-25
12
448 Views
Last Modified: 2012-05-11
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
Comment
Question by:befidled
  • 6
  • 3
  • 3
12 Comments
 
LVL 4

Expert Comment

by:m_walker
ID: 35213607
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
 
LVL 1

Author Comment

by:befidled
ID: 35213697
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
 
LVL 4

Expert Comment

by:m_walker
ID: 35213759
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 1

Author Comment

by:befidled
ID: 35213762
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
 
LVL 1

Author Comment

by:befidled
ID: 35213780
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
 
LVL 4

Expert Comment

by:m_walker
ID: 35213910
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
 
LVL 1

Author Comment

by:befidled
ID: 35216119
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
 
LVL 31

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:befidled
ID: 35220571
Won't that rename every URL on my site with a .xml suffix?
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 35220594
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
 
LVL 1

Author Closing Comment

by:befidled
ID: 35220603
That's good. I used a modified version of that solution.

thanks.
brian
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35220606
You are most welcome
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question