views:

328

answers:

4

Hello

I am having problems getring a simple redirect statement to take effect on my Godaddy account. I have the following statements in my .htaccess file:

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/lists/$ / [R=301]

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/blog/$ http://myotherdomain.net/ [R=301]

The 1st redirect ALWAYS work. The 2nd and 3rd ones however, NEVER work. I just get a 404 from the server. The Apache logs do not reveal any useful infomation - just a 404.

Any ideas, anybody? Your help will be greatly appreciated. Thanks

A: 

Put the first one last. Once it encounters a redirect match, it runs it and ignores the rest.

Chris Ballance
A: 

For simple redirects like that, better use the simple RedirectMatch directives:

RedirectMatch 301 ^/lists/$ http://mydomain.net/
RedirectMatch 301 ^/blog/$ http://myotherdomain.net/

If you insist on using rewriting make sure you add the L flag to your rules.

Apache mod_rewrite Flags says :

You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.

kmkaplan
+1  A: 

Per-directory Rewrites
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

So just leave the leading slash out of the pattern.

Gumbo
+1  A: 

Simply remove the slashs at the beginning. It also might be useful to make the slashs at the end optional.

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^lists/{0,1}$ / [R=301]

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^blog/{0,1}$ http://myotherdomain.net/ [R=301]
slosd