Forum Moderators: phranque
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]
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
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]
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]
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
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. :)