Solved

Can I use htaccess to add suffix to URL string?

Posted on 2011-03-25
12
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

626 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