Forum Moderators: phranque

Message Too Old, No Replies

Redirecting directory with .htaccess

         

WebWalla

10:46 am on Nov 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have 2 domain names, mydomain.com and my-domain.com. My current .htaccess redirects all requests for the hyphenated version to the non-hyphenated version, not just for the domain name, but for all URLs from my web ...
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTP_HOST} my-domain.com$ [NC]
RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]
</IfModule>

I had this done for me, since I'm no expert. How can I now change this to redirect the following ...
mydomain.com/directory to newdomain.com/directory

Another possibility I may want to do is to redirect ...
mydomain.com/directory to newdomain.com

All help much appreciated. Thanks.

[edited by: jdMorgan at 2:00 pm (utc) on Nov. 3, 2003]
[edit reason] Delinked [/edit]

closed

4:59 am on Nov 2, 2003 (gmt 0)

10+ Year Member



From this forum's charter:

Please do not post large blocks of code, or ask us to write your code for you.

It might help if you took a stab at it first, and posted what you came up with.

Here are the pages I read when I was just starting out:

Regular expressions [etext.lib.virginia.edu]
mod_rewrite [httpd.apache.org]

WebWalla

12:22 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I've come up with these 2 lines in an .htaccess which I have installed in a test directory for the moment (I presume it therefore overides the global .htaccess) ...

RewriteCond %{HTTP_HOST} mydomain.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/newdirectory/$1 [R=301,L]

This seems to be working fine when the URL includes the actual file requested (e.g. mydomain.com/testdir/test.html is redirected to newdomain.com/newdirectory/test.html)

However, when only the directory with no filename is specified I have 2 different problems depending on whether the trailing slash is used.

If the trailing slash is used in the request (e.g. mydomain.com/testdir/), I get a 403 error, even though index.htm exists.

If it is not used (e.g. mydomain.com/testdir), a symbolic link is taken as the destination (e.g. newdomain.com/newdirectory//home/virtual/site20/fst/var/www/html/testdir).

What am I doing wrong? Thanks.

[edited by: jdMorgan at 2:01 pm (utc) on Nov. 3, 2003]
[edit reason] Delinked [/edit]

The Contractor

12:36 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure why. I use the following one-line to redirect from an old to new directory (if files are named the same as old they will be found):

Redirect 301 /olddirectory [domain.com...]

WebWalla

1:47 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, much simpler - thanks.

jdMorgan

2:13 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WebWalla,

>If the trailing slash is used in the request (e.g. mydomain.com/testdir/), I get a 403 error, even though index.htm exists.

A server status code of 403 means that access is forbidden. It says nothing about whether the file exists.

> If it is not used (e.g. mydomain.com/testdir), a symbolic link is taken as the destination (e.g. newdomain.com/newdirectory//home/virtual/site20/fst/var/www/html/testdir).

Your mod_rewrite code, as written above, cannot do this.

Adding these two bits of evidence, I suspect you have an incorrect directive -- either in your web root .htaccess, or in the server's httpd.conf file -- that is causing this problem. You may want to ask your host to check httpd.conf, since this misconfiguration will probably cause other problems.

Jim

WebWalla

7:57 am on Nov 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your mod_rewrite code, as written above, cannot do this.

Would this cause it?
Options +FollowSymLinks

jdMorgan

1:48 pm on Nov 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Options +FollowSymLinks is required to "enable" mod_rewrite. I'd put my money on a server configuration problem here...

Jim