Solved

Rewrite additional extension

Posted on 2009-05-07
3
200 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now