Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite confusion

         

Perfection

7:32 pm on Jun 26, 2006 (gmt 0)

10+ Year Member



In my root directory my .htaccess files contains this:

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

Now when someone requests mydomain.com/blah.html it redirects to www.mydomain.com/blah.html (WITH the www).

Today I installed wordpress. It's located at www.mydomain.com/blog.

What I'm trying to do now is make it so a request for mydomain.com/blog (or mydomain.com/blog/2006-post-title/ gets redirected to www.mydomain.com/blog/whatever (WITH the www).

I'm assuming it should be done with a new .htaccess file in the blog directory, right? I've tried for a while but the closest I can get is having mydomain.com/blog redirect to www.mydomain.com. Apparently I'm doing something wrong.

Help would be greatly appreciated. Thanks.

[edited by: jdMorgan at 10:38 pm (utc) on June 26, 2006]
[edit reason] example.com [/edit]

jdMorgan

8:13 pm on Jun 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you posted should already do the redirect you need. So, the fact that it doesn't do so indicates that the WordPress installation involves some code that it causing the existing rule to be by-passed. The most likely cause is that an Alias directive has been used to point WP requests to a different directory path than is used for your existing pages.

Or perhaps it installed a rewriterule in your .htaccess file positioned *above* the domain redirect. Either way, it's down to something interfering with your existing rule.

Without more info on the WordPress/server setup, it's impossible to tell. If you are getting errors, take a look at the server error log; it may be quite helpful.

Jim

Perfection

8:20 pm on Jun 26, 2006 (gmt 0)

10+ Year Member



Jim, you are correct. Wordpress requires this code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /blog/index.php [L]
</IfModule>

Is this what's causing the problem?

jdMorgan

9:05 pm on Jun 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where is that WordPress rewrite code located?

Where is you domain redirect code located?

(httpd.conf, conf.d, .htaccess?)

Jim

Perfection

9:10 pm on Jun 26, 2006 (gmt 0)

10+ Year Member



The domain redirect code is in .htaccess in my root directory.

The wordpress rewrite code is also supposed to be in .htaccess, although I'm not sure if it should be in the one in the root directory, or in a new one in the /blog directory.

jdMorgan

9:34 pm on Jun 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are saying that both pieces of code are in the same .htaccess file, then simply move the domain redirect *above* the WordPress code.

You may delete any redundant code, such as multiple RewriteEngine on and Options directives -- Only one each of these is needed at the top of the mod_rewrite code.

Jim

Perfection

9:53 pm on Jun 26, 2006 (gmt 0)

10+ Year Member



Jim, this is what my whole .htaccess looks like after making those changes. Again, it's in the root directory.

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

Should everything work correctly now?

Thanks again for your help.

[edited by: jdMorgan at 10:39 pm (utc) on June 26, 2006]
[edit reason] example.com [/edit]

jdMorgan

10:12 pm on Jun 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Should everything work correctly now?

I'd encourage you to test it, rather than asking a question here which only testing can answer... :)

Jim

P.S. You can delete the <IfModule> container. It's redundant in that if mod_rewrite isn't loaded, then the code won't run. But I think you'd *want* the resulting error message if mod_rewrite wasn't loaded, rather than a silent failure.

Perfection

11:00 pm on Jun 26, 2006 (gmt 0)

10+ Year Member



Good idea. =]

I did some testing and everything appears to be working correctly.

So, once again, thank you.