Welcome to WebmasterWorld Guest from 54.144.243.34

Forum Moderators: Ocean10000 & incrediBILL & phranque

mod rewrite setting up permanent redirect

for a subdomain, is not working as it should =(

   
11:12 pm on Mar 13, 2007 (gmt 0)

5+ Year Member



My situation is this:

www.domain.com (main website)

sub.domain.com/files/ (hosted files in past)
sub.domain.com/images/ (hosted images in past)

Using htaccess I wish to setup a permanent redirect from:

sub.domain.com/files/anyfile.ext

to:

www.otherdomain.com/router.php?request=anyfile.ext

This is the contents of the .htaccess file I have in my sub.domain.com directory on my host:

Options All -Indexes
Options +FollowSymLinks

RewriteEngine On
RewriteRule ^files/(.*)$ [otherdomain.com...] [R=permanent,L]

When I use this, it throws an Internal Server Error. I'm stumped, can anyone shed some light on the problem?

11:23 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



What is in your server error log?

You code, though not completely correct, is syntactically correct enough that it shouldn't be throwing a server error for any obvious reason.

Jim

12:02 am on Mar 14, 2007 (gmt 0)

5+ Year Member



Nothing in the error log indicated the problem =(

After trying numerous examples, I found this to work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.sub.domain.com$
RewriteRule ^(.*)$ [otherdomain.com...] [R=301,L]

However, I'd optimally it should only redirect when the request is from

sub.domain.com/files/somefile.ext

and in the rule i would like it to write the somefile.ext to the end of file.php, such as

[otherdomain.com...]

I tried setting that up to work but it continues to just redirect everything from the subdomain..

12:21 am on Mar 14, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If your code is located in /files/.htaccess, then it won't affect anything but the files in that directory and below. You can eliminate one (and correct the other) RewriteCond, and add the parameter you want as shown:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule (.*) http://www.otherdomain.com/file.php?request=$1 [R=301,L]

Note the optional "www.", the required escaping of literal periods, the removed end anchor (to prevent problems if a port number is appended to the domain), the removed (redundant) anchoring on the RewriteRule pattern, and the $1 back-reference to the requested page.

If the requested page is blank, i.e. the request is for /files/, then the request= value will also be blank. If that is a problem, it can be addressed by using two rules, but if not, just stick with this one.

Jim

[edited by: jdMorgan at 12:22 am (utc) on Mar. 14, 2007]

1:48 am on Mar 14, 2007 (gmt 0)

5+ Year Member



Thanks, that's very helpful. I am quite mediocre with regex or reading most regex documenation, so the explanation with example is more help than anything I've come across yet.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month