Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod_rewrite problem (again)

     
2:57 pm on Dec 12, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 20, 2003
posts:8
votes: 0


Hi, one of my sites is a free redirection service (by subdomains). I am trying to have apache do as much as possible for me in the .htaccess file including making it easier to set up path forwarding; this is my .htaccess

rewriteEngine on
rewriteCond %{HTTP_HOST} ^(www\.seconddomain\.org¦seconddomain\.org)$
rewriteRule ^(.*)$ [primarydomain.net...] [L,R=permanent]
rewriteCond %{HTTP_HOST} ^www\.primarydomain\.net$
rewriteRule ^(.*)$ [primarydomain.net...] [L,R=permanent]
rewriteCond %{HTTP_HOST} ^primarydomain\.net$
rewriteRule ^$ /start.php

And when I add this to have index.php (this one handless all redirection and path forwarding) called I get a internal server error; bah.

rewriteCond %{HTTP_HOST}!^primarydomain\.net$
rewriteRule ^(.*)$ /index.php

If you understand the above lines properly can you please explain why it breaks it? :) In short I want it to rewrite all requests made when the domain is different from primarydomain.net to index.php so I can use the server variables and do pathforwarding. Without the last two lines: no path forwarding unless I set it up using 404 errors; but still this would not work if the path is the same as an existing file on the site itself :/

5:35 pm on Dec 12, 2003 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


You can 'compress' this first part:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?seconddomain\.org¦www\.primarydomain\.net
RewriteRule .* http://primarydomain.net/ [L,R=301]
RewriteCond %{HTTP_HOST} ^primarydomain\.net
RewriteRule ^$ /start.php [L]

This second part requires a space between "}" and "!" -- Otherwise, it looks OK. You might also want to make sure the hostname is not blank (for HTTP/1.0 requests).

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^primarydomain\.net
RewriteRule .* /index.php [L]

Another thing I see is that you may be setting yourself up for an infinite loop, should any subsequent rules perform internal subrequests. I'd suggest you add [L] flags to all RewriteRules, and also exclude the target file from the rewrite by adding a third RewriteCond:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^primarydomain\.net
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule .* /index.php [L]

Since you are not using back-references in any of your rules, you can change the patterns from ^(.*)$ to just ".*" to save a bit of processing time.

I'd recommend that you *do not* end-anchor hostnames. It causes problems on some servers.

If you're still having problems, take a look at your server's raw error logs to see what the problem is -- sometimes the error log is very helpful with rewrite errors (and sometimes not).

Jim

2:04 pm on Dec 13, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 20, 2003
posts:8
votes: 0


-----
You can 'compress' this first part:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?seconddomain\.org¦www\.primarydomain\.net
RewriteRule .* [primarydomain.net...] [L,R=301]
-----
Actually, I dont think I can. Notice what I am using; when user enters by www.primarydomain.com/somefile I redirect to primarydomain.com/somefile, if he enters through secondarydomain.com/whatever I just redirect to primarydomain.com/ - your code does not seem to carry on the ressource to the primary domain :-)
----
This second part requires a space between "}" and "!" -- Otherwise, it looks OK. You might also want to make sure the hostname is not blank (for HTTP/1.0 requests).
----
I should have stated that in the original post, I do have a space between the } and! but for some reason it removed that space when I pasted the code :/ (edit: hmm, I put a space before this:!)

-
Your other tips are very usefull, I will try and remove end-tag tomorrow when I wake up :-) (Have no further time today)

-Bjarne

9:23 pm on Dec 13, 2003 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


Yeah, I missed that part about the difference in keeping the resource vs. redirecting to the home page... Unlike you, I didn't have the good sense to get some rest... :)

You can still use this trick to compress the www- and non-www- domains into one pattern:


RewriteCond %{HTTP_HOST} ^(www\.)?seconddomain\.org

Let us know how the rest of it works out.

Jim