homepage Welcome to WebmasterWorld Guest from 54.227.25.58
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

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

Apache Web Server Forum

    
mod rewrite setting up permanent redirect
for a subdomain, is not working as it should =(
harleyx




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

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?

 

jdMorgan




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

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

harleyx




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

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..

jdMorgan




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

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]

harleyx




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

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.

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