Solved

Rewrite additional extension

Posted on 2009-05-07
3
212 Views
Last Modified: 2012-05-06
I want to rewrite all urls so so the extension .txt is appended (unless it already has a .txt extension)

This
     hello.php
should be re-written to
     hello.php.txt

This
     hello
should be re-written to
     hello.txt

This
     hello.js
should be re-written to
     hello.js.txt

This
     hello.txt
should NOT be rewritten


       
0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 8

Expert Comment

by:thetmanvn
ID: 24328183
If you give more details about your case, it will be more accuracy

From your needs, add two rules to your rewrite portion:

RewriteRule ^(.*\.txt)$ $1 [L]
RewriteRule ^(.*\..*)$ $1\.txt [L]
0
 
LVL 16

Author Comment

by:hankknight
ID: 24328226
How can I make it so

test.txt

does not go to

test.txt.txt

?
0
 
LVL 8

Accepted Solution

by:
thetmanvn earned 500 total points
ID: 24328293
Test my rule and you've got it. (You can add this to .htaccess in one test folder to test yourself, create 3 file a.txt, b.php and c.js in the same folder with .htaccess, browse 3 files with your browser and you will see the result)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*\..*)$ $1\.txt [L]
</IfModule>

1./ The first rule RewriteRule ^(.*\.txt)$ $1 [L] check whether the URI  end with ".txt", in this case, it remain the URI, so test.txt does not go to test.txt.txt.   The [L] Flag tell this is the last rule so the second rule will not be processed

2./ If the file does not end with .txt so the first rule was bypassed, then come to the second rule, it check whether URI has extension, if yes it'll add .txt to end of URL, so b.php -> b.php.txt, c.js -> c.js.txt
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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