Forum Moderators: phranque

Message Too Old, No Replies

redirecting home page

using mod_rewrite or mod_alias and having issues

         

stevebu56

5:05 pm on Aug 15, 2011 (gmt 0)

10+ Year Member



I am trying to figure out what the correct way to redirect my homepage is and have been using mod_alias RedirectMatch with some success. I am running a Wordpress backed site and have their cononicalization redirecting turned on and have my wp-content split out into a different directory. That's why there is the wp_core subdirectory in the rules. My htaccess file is as follows.


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ /wp_core/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /wp_core/index.php [L]
RedirectMatch 301 ^/$ http://example.com/blog/
</IfModule>


This is successfully redirecting the home page to /blog but when I try to preview things with a URL that looks like http://example.com/blog/?p=55&preview=true the browser says redirect loop. Can anyone explain this to me?

A little more background. I have a product that I am blogging about on http://example.com/blog/ and I want the homepage to redirect to that while I develop it. Once it is done I will put up a page at http://example.com. Is the route that I'm taking ok or is there a better way to set this up?

lucy24

10:34 pm on Aug 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Uhm, how did that mod_alias rule (RedirectMatch...) sneak into your mod_redirect package? See any post by g1smd for the extreme perils of combining the two. (Ignore any post by me on the same subject.)

lucy24

1:06 am on Aug 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When my fingers typed mod_redirect my brain meant mod_rewrite. Sorry 'bout that.

stevebu56

1:40 am on Aug 16, 2011 (gmt 0)

10+ Year Member



Thanks I'll take a look at g1smd's threads.

stevebu56

1:21 am on Aug 19, 2011 (gmt 0)

10+ Year Member



With a little digging on this site and running across some best practices regarding not mixing RewriteMatch and RewriteRule I was able to fix my problem and get it working. This is what my htaccess file looks like.

Any errors or potential problems in this?


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Redirect home page to /blog but only if not trying to preview a
# post from the admin site.
RewriteCond %{QUERY_STRING} !preview=true
RewriteRule ^$ http://example.com/blog/ [R=301,L]

RewriteRule ^$ /wp_core/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /wp_core/index.php [L]
</IfModule>

lucy24

2:52 am on Aug 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Got a nasty feeling there's some entangling of Rules and Conditions here. In general, leave a space after each RewriteRule. Not for aesthetics and not for coding necessity, but because it helps you to keep them conceptually separated.

Does the first Rule work? On my server it wouldn't, because anything asking for example.com has already been changed by some invisible process into example.com/index.html (I learned this only after prolonged swearing at htaccess for not doing what it was supposed to do.)

Is the second rule just for (I assume) you? That is, you've skimmed off the people not asking for a preview, so that leaves only the ones who are asking for a preview.

Here's where you need a space to stress that Rule #3 is entirely separate. What is it intended to do? For starters you don't want .* because that admits null inputs, and your two previous rules have got rid of all those. Since you're not capturing, all you need is a single . meaning "any request whatsoever".

So anyone asking for any nonexistent file of any kind will be sent to example.com/wp_core/index.php ? The same place you go to read previews?

That's why I wondered about the Conditions and Rules.


I'm getting fond of that WP boilerplate "IfModule mod_rewrite.c" etc. If mod_rewrite is on vacation and the If/Then weren't there, what would happen? Would mod_access step in and say "Well, it's not really my area of expertise, and I'm officially retired, but I'll give it a shot"? :)

stevebu56

2:37 am on Aug 25, 2011 (gmt 0)

10+ Year Member



Sorry for the late reply, but for completeness here's some answers to your questions.

Does the first Rule work?

As far as I can tell yes users are redirected to the /blog URL and when I turned on debugging for mod_rewrite I didn't see any extra index.html being added.

Is the second rule just for (I assume) you? That is, you've skimmed off the people not asking for a preview, so that leaves only the ones who are asking for a preview.

Correct, this is only for me when creating blog posts and pressing the preview button in Wordpress. In this case I don't want it to redirect to /blog since that results in the preview not working since those URLs are off of just the example.com.

So anyone asking for any nonexistent file of any kind will be sent to example.com/wp_core/index.php ? The same place you go to read previews?

Correct, everything else is going there and Wordpress takes care of redirecting or generating the correct URL. The last section of the htaccess file with the two RewriteRules in it I took directly from this article at the Wordpress codex site. [codex.wordpress.org]

g1smd

8:24 am on Aug 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Dump the two lines with "ifModule" in them. You don't want rewrite failures to fail silently.

Add a blank line after each and every RewriteRule.

Add a plain English #Comment before each of the three chunks of code explaining what each one does (for when you look at the code in three months time and have no idea what it does).