Forum Moderators: phranque

Message Too Old, No Replies

what is wrong with my rewrite rules?

         

h2ojunkie

11:40 am on Jan 25, 2006 (gmt 0)

10+ Year Member



I've searched the forum and tried out dozens of suggested solutions, but I jsut can't find one that works.

Here's the situation.

I have mydomain.com

I also have several other domains parked on mydomain.com

ie. mydomain.net mydomain.org etc.

I want to 301 anyone that access my website to:
www.mydomain.com no matter what domain they use to get there.

In my root .htaccess file I have:


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) [mydomain.com...] [L,R=301]

This works perfectly site wide with one exception.

In one sub directory I have another rewrite rule so I have to duplicate my root htaccess for that subdirectory, plus add in the new rewrite rule. This is what I have in the subdirectory .htaccess


Options +FollowSymLinks
RewriteEngine on

RewriteBase /forum
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) [mydomain.com...] [L,R=301]

RewriteRule ^f([0-9]+)p([0-9]+)\.html$ forumdisplay.php?forumid=$1&page=$2 [QSA,L]

This works fine for everything, except when the trailing slash is missing on a url that is being rewriten.

For example:

www.mydomain.com/forum - RewriteCond does not match so apache adds the trailing slash on its own and redirects to www.mydomain.com/forum/ and it works fine

www.mydomain.net/forum/ - RewriteCond matches and redirects to www.mydomain.com/forum/ and works fine

www.mydomain.net/forum - RewriteCond matches but it does not work, and redirects to [mydomain.com...]

Anyone have any idea what I'm missing here?

jdMorgan

4:25 pm on Jan 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



h2ojunkie,

Welcome to WebmasterWorld!

> I have to duplicate my root htaccess for that subdirectory

Generally, this is not true. See mod_rewrite RewriteOptions [httpd.apache.org] inherit.

You may need to add code to fix the trailing slash before doing any other rewriting. We have many threads on that subject here.

Jim

jd01

1:33 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something interesting about this:

http://www.mydomain.com/forum//home/mydomain/public_html/forum

I have run into a couple of situations where the full directory structure has been appended to a rule... in both cases the .htaccess was outside the /public_html/ (/htdocs/ or /www/, etc.) directory.

In both cases the oddities were corrected when the .htaccess was moved inside the 'public' directory.

Justin

h2ojunkie

3:22 am on Jan 26, 2006 (gmt 0)

10+ Year Member



You are right, the problem is caused by having the rules in /public_html/forum/

But this rule only applies to files in the /forum/ directory so I need to have the htaccess there, unless you have another suggestion.

RewriteRule ^f([0-9]+)p([0-9]+)\.html$ forumdisplay.php?forumid=$1&page=$2 [QSA,L]

Thanks.

jd01

3:27 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could either put the .htaccess in the /forum/ directory, or you could prepend the left side of your rule with forum/

RewriteRule ^forum/f([0-9]+)p([0-9]+)\.html$ /the/path/to/forumdisplay.php?forumid=$1&page=$2 [QSA,L]

If forumdisplay.php is in /forum/, then the right side of your rule would be:
/forum/forumdisplay.php?forumid=$1&page=$2

(I always recommend using a server relative path on the right side of the rule... sometimes things don't work like they should if you use a directory relative path.)

Hope this helps.

Justin

jdMorgan

3:32 am on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case,

RewriteRule ^forum/f([0-9]+)p([0-9]+)\.html$ /forum/forumdisplay.php?forumid=$1&page=$2 [QSA,L]

should work if placed in the top-level directory. It would be more efficient to have your original code in the /forum subdir itself, but what good is that if it won't work?

With that in mind, I suppose you could also try:


RewriteRule ^f([0-9]+)p([0-9]+)\.html$0 [b]/f[/b]orum/forumdisplay.php?forumid=$1&page=$2 [QSA,L]

in the /forum subdir. Note the only change here is the document-rooted substitution, rather than a relative path.

It should have worked as you had it, though, so there's probably a configuration error... Your choice to either try to get that resolved or just work around it.

I assume that you sometimes have requests for /forum/f<digits>p<digits>/html?some_query_string_here
If not, then you don't need the [QSA] flag, as it's purpose is to retain the original query string and append the new query data to that.

Jim

h2ojunkie

4:22 am on Jan 26, 2006 (gmt 0)

10+ Year Member



removing the subdir htaccess and putting this in the root htaccess worked.

RewriteRule ^forum/f([0-9]+)p([0-9]+)\.html$ /forum/forumdisplay.php?forumid=$1&page=$2 [QSA,L]

I will try the other suggestion later tonight, as this is a busy site and anything I can do to be more efficient is good for the server load.