Solved

Rewrite additional extension

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

21 Experts available now in Live!

Get 1:1 Help Now