Forum Moderators: phranque

Message Too Old, No Replies

Directory to Directory rewriting

         

Hemeno

10:27 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



I've searched for different methods to do this on the forums, but the only thing that I have been able to accomplish is infinite loop errors.

This is my .htaccess file so far:

ErrorDocument 404 /notfound.html
Options -Indexes

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

I want to add to this so that I can change this directory:
www.example.com/directory1/

to

www.example.com/directory2/

I know it has to be easy, but it seems like the code for changing all non www links is giving me errors.

Thanks for the help.

[edited by: jdMorgan at 4:58 am (utc) on July 27, 2005]
[edit reason] Examplified. [/edit]

jdMorgan

11:22 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to add to this so that I can change this directory:
www.mysite.com/directory1/ to www.mysite.com/directory2/

The code you posted should do a domain redirect from mysite to www.mysite, but that has nothing to do with changing directories. Did you post all of your code? We can't discuss your subdirectory redrect if you don't post it...

See the references cited in our forum charter [webmasterworld.com] for more info.

Jim

Hemeno

4:22 am on Jul 27, 2005 (gmt 0)

10+ Year Member



ErrorDocument 404 /notfound.html
Options -Indexes

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.net\directory1\$ [NC]
RewriteRule ^(.*)$ http://www.example.net/directory2/$1 [R=301,L]

This is what I came up with. It's definetly screwed up....which is why I didn't include it. The first part of the code works just fine. It redirects non-www urls to www urls as well as provide an error page for 404 errors.

[edited by: jdMorgan at 4:58 am (utc) on July 27, 2005]
[edit reason] Examplified. [/edit]

jdMorgan

4:46 am on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.n[b]et[/b] [NC]
RewriteRule (.*) http://www.example.net/$1 [R=301,L]
#
RewriteRule ^[b]directory1/[/b](.*)$ http://www.example.net/directory2/$1 [R=301,L]

1) Do not end-anchor the domain name, otherwise, an appended port number will break your rule, e.g. a request for http://example.net:80/index.html would cause the redirect to fail to be invoked, because HTTP_HOST would contain "example.net:80" and your pattern, end-anchored with "$", would require exactly "example.net" and would not match.

2) You cannot mix the local URL-path and the HTTP_HOST; HTTP_HOST will never contain anything but the requested hostname and an optional port number. So, your second RewriteCond pattern would never match with a URL-path in it. Also, since the first rule will redirect any requests for example.net to www.example.net, the second rule does not need any RewriteCond which tests for example.net; Including it would actually cause the rule to fail, because the first rule will have already redirected the request to www.example.net/directory1/whatever.

Note that the backslashes are used to escape literal characters which would otherwise act as regular-expressions pattern-matching tokens. For example, without a preceding backslash, "." is taken to mean, "any single character." In mod_rewrite patterns, you must escape the following: ^$?.+*()[]{}¦\ and I may have forgotten a few.

Our forum charter contains links to some resources which may be useful to you.

Jim

Hemeno

6:08 am on Jul 27, 2005 (gmt 0)

10+ Year Member



Thanks for the advice, but I still have a problem.

If I type www.example.net/directory1/
the redirect works.

If I type www.example.net/directory1
the redirect doesn't work.

If I type www.example.net/directory1/example.php?t=27
the redirect doesn't work.

I'm in the process of reading some of the links in the charter. My eyes end up glazing over half-way through, so it will take a while before I master this. :)

jdMorgan

1:13 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To fix the problem for the missing trailing slash, make it optional:

RewriteRule ^directory[b]1/?([/b].*)$ http://www.example.net/directory2/$1 [R=301,L]

For the php problem, please state what Apache version you're running, and whether you have httpd.conf access.

Jim