homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

If SWF then gohere.htm
swf then redirect

 1:21 pm on Jun 17, 2007 (gmt 0)

How would I write a mod_rewrite rule such as this?

--If a person tries to open any *.swf file-- (by typing in the address bar of browser)

--Then redirect them to gohere.htm or http://www.example.com--


[edited by: jdMorgan at 5:01 pm (utc) on June 17, 2007]
[edit reason] example.com [/edit]



 8:57 pm on Jun 17, 2007 (gmt 0)

How would I write a mod_rewrite rule such as this?
--If a person tries to open any *.swf file-- (by typing in the address bar of browser)

--Then redirect them to go_here.htm or http://www.example.com--


I read some of the rules while browsing some of the other posts and didn't know I needed to make an attempt at coding. Sorry. Here's what I've come up with. But I get a '500 Internal Server Error' with this.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^*\.swf$ http:www.go_here.com [R=301,L]


 12:53 am on Jun 18, 2007 (gmt 0)

Well I accomplished what I wanted to do but I still have problems. I will go more into detail. I have swf flash games on my website (I didn't write them so I can't alter the code (and don't want to)). I have banners on the htm pages that calls the swf files in order to pay for hosting them. But if a person wants to go directly to the game and do this -- http://example.com/game.swf -- instead of http://example.com/game.htm, no ads are shown. So I did this....

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*).swf$ http://www.example.com/game.htm [R=301,L]

and it works. Oh, happy day!

But...now if I call the game in the normal way --http://mysite.com/game.htm-- the rewrite rule does something concerning the swf file, I'm not sure what. But the rewrite rule causes the swf to not load. Is there a solution for this? Thanks again.

[edited by: chasmcg at 1:02 am (utc) on June 18, 2007]

[edited by: jdMorgan at 1:35 am (utc) on June 19, 2007]
[edit reason] example.com [/edit]


 12:58 am on Jun 18, 2007 (gmt 0)

You must rewrite your rule according to an empty HTTP_REFERER:

RewriteCond %{HTTP_REFERER} ^$
RewriteRule ...etc...


 1:17 am on Jun 18, 2007 (gmt 0)

Thanks a lot, Vince, Works great! Exactly what I need. I will now look up....RewriteCond %{HTTP_REFERER} ^$.... to see what I'm doing.


 12:32 pm on Jun 18, 2007 (gmt 0)

One other question. If a user requests http://example.com/game.swf, how could I send him to the appropriate htm file? In the instance above, I would send the user to http://example.com/game.htm. But in real time I would never know what swf file he had requested. Hope I'm being clear. Thanks.

[edited by: jdMorgan at 1:35 am (utc) on June 19, 2007]
[edit reason] example.com [/edit]


 2:12 pm on Jun 18, 2007 (gmt 0)

RewriteRule ^(.*)\.swf$ $1.htm [R=301,L]


 2:46 pm on Jun 18, 2007 (gmt 0)

Here is a speed-up and robustness improvement:

RewriteRule ^(([^.]+\.)+)swf$ http://www.example.com/$1htm [R=301,L]

This eliminates the ambiguous ".*" pattern so that we can do a single-pass, left-to-right pattern match, and by specifying the full canonical substitution URL, will work regardless of the UseCanonicalName server setting.



 4:49 pm on Jun 18, 2007 (gmt 0)

Vince and JD, thanks a lot. You have been a great help. One more question and I hope this is the last. I actually have the swf files and htm files in different folders (see below).


You have this to redirect to the same directory.

http://www.example.com/$1htm [R=301,L]

How would I code the line above to go back (up) one directory? Thanks again.

[edited by: jdMorgan at 1:36 am (utc) on June 19, 2007]
[edit reason] example.com [/edit]


 1:04 am on Jun 19, 2007 (gmt 0)

RewriteRule ^(([^.]+\.)+)swf$ ../$1htm [R=301,L]


 7:30 am on Jun 19, 2007 (gmt 0)

RewriteRule ^(([^.]+\.)+)swf$ ../$1htm [R=301,L]

Thanks Vince for the reply. I tried that along with ../../ and it doesn't work for me. It works in the sense that it goes back 1 directory (or 2 with ../../) but it always has the swf/game.htm included. I can't seem to lose the swf directory no matter what I try.


 7:43 am on Jun 19, 2007 (gmt 0)

RewriteRule ^/swf/(.*)\.swf$ /$1.htm [R=301,L]
RewriteRule ^swf/(.*)\.swf$ /$1.htm [R=301,L]


 12:58 pm on Jun 19, 2007 (gmt 0)

RewriteRule ^swf/(.*)\.swf$ /$1.htm [R=301,L]

Vince, the last line of code works. Thanks a lot!

Seems you solve a problem for me and I discover another one. I notice that if I implement the code above when the swf file is not in the browser cache, the code works. If the swf file is in the cache, it doesn't. But it does exactly what I was wanting, so again, thanks a lot for all your help.


 1:01 pm on Jun 19, 2007 (gmt 0)

If the file is in the cache, then yes, the code won't work - the only way around it is to configure your server to ensure that the file is not cached. This will lead to a big slowdown for your regular users and a big bandwidth increase for your server.


 1:46 pm on Jun 19, 2007 (gmt 0)

Yes, I was thinking about the slowdown. I've read that the .htaccess file can slow down your server as well.

Speaking of slowdown. I may try to implement a lot of server-side includes on my site. Google is telling me I can't serve their ads in iframes. I have iframes all over my site and will have to do something or Google will suspend me. I already have some SSIs in a few shtml files but want to use them with a .htaccess file. This will keep from having to rename all my files to shtml and screw up all my links to them. I've read about the code below to add to a .htaccess file but I'm concerned about the slowdown and the parsing.

"AddType text/html .shtml .shtm .htm .html"

"AddHandler server-parsed .shtml .shtm .htm .html"

How bad will that slow down my server? Or is there a better way to do SSIs without renaming all of the files to shtml? Thanks.


 3:03 pm on Jun 19, 2007 (gmt 0)

If you use that AddHandler directive, you won't need to rename your files -- and you should never rename your pages or files anyway. It's almost never needed if you have mod_rewrite available.

Another way to selectively enable SSI parsing on text/html pages is to use the XBitHack directive in Apache mod_includes. With XBitHack enabled, the server will examine the X-bit (eXecute permissions bit) of each html file. If the bit is set, the file will be parsed for SSI. If not, it is handled as a static page and no parsing is done.

See Apache mod_includes for the details.



 12:59 am on Jun 20, 2007 (gmt 0)

Thanks, JD for the reply. I was looking into the XbitHack earlier. But since I will probably have SSIs on most of my pages I may as well do the easier way (not have to worry about what page has an SSI, which doesn't). I'm kind of concerned about the slowdown. But I've already added the handler directives to the main .htaccess and it doesn't seem to be any slower. I'm sure it is a bit slower but I can't tell the difference.


 5:30 am on Jun 20, 2007 (gmt 0)

Without SSI parsing, the server simply grabs a file and sends it.

With SSI parsing enabled, the server has to read through the file and look for a "<!--#" sequence marking an SSI directive, then process that directive, and resume looking for another, repeating until the end-of-file is reached.

But the SSI directives are usually very simple, and since SSI is so "mature," the functions are fairly-well optimized.

So, although there are dire warnings about server performance degradation, we need to remember that this stuff was written when CPUs were ten thousand times slower and memory a million times smaller. There are many, many, really-big sites today built entirely on PHP and similar server-side scripting languages, and PHP is certainly no faster to parse than SSI is...

Before abandoning the XBitHack approach, take a long look at the effects that both methods have on the Last-Modified server response header. With XBitHack, you can retain Last-Modified functionality, whereas with straight .shtml, you cannot. This affects the cacheability of your pages, and ultimately, your server load.



 5:44 am on Jun 20, 2007 (gmt 0)

By the way, on the original question, a simple-but-by-the-book way to code the rule would be:

RewriteRule ^swf/([^.]+)\.swf$ http://www.example.com/$1.htm [R=301,L]

This form of the rule will work even if your host changes the setting of UseCanonicalName to 'on', or if you move to a different host that sets it to 'on'.



 5:45 am on Jun 20, 2007 (gmt 0)

The only way around this is to check the Last-Modified yourself. If there is a HTTP_REFERER which is valid, and there is a IF MODIFIED SINCE then return 304-Not Modified.

That would mean that those who have it cached AND are opening it from the HTML page will not be forced to reload it.

[edited by: jdMorgan at 12:16 pm (utc) on June 20, 2007]
[edit reason] Fix typo. [/edit]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved