Forum Moderators: phranque
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 onRewriteBase /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?
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
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
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.
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
RewriteRule ^forum/f([0-9]+)p([0-9]+)\.html$ /forum/forumdisplay.php?forumid=$1&page=$2 [QSA,L]
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]
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
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.