Jump to content

.htaccess Rewriteengine/rewriterule


Recommended Posts

I don't know very much about .htaccess and rewrite rules, so I've cobbled this together after several days of trial and error.

 

Basically, I decided I didn't like my blog on a subdomain (blog.mysite.com) and I wanted to streamline it with the rest of the site (mysite.com/blog/). I also wanted to change all the dynamic URLs (mysite.com/blog/archives/view.php?date=2005-02-10&keyword=puppy) to clean URLs (mysite.com/blog/archives/2005/02/10/puppy)

 

This is how my .htaccess ended up (it's saved in the /blog/ folder):

 

Options All -Indexes

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^blog.mysite.com$ [OR]RewriteCond %{HTTP_HOST} ^www.blog.mysite.com$

RewriteRule ^(.*)$ http://mysite.com/blog/$1 [R=301,L]

 

RewriteRule ^archives/(.*)/(.*)/(.*)/(.*)$ archives/view.php?date=$1-$2-$3&keyword=$4

 

This works, so I'm basically happy, but I'm just trying to understand the logic of this stuff. Since I am getting rid of the subdomain, I assume the first two RewriteCond and the first RewriteRule have to be in the /blog/ folder in case someone does try to access blog.mysite.com--the /blog/ is acting something like a root folder until the URL redirects it back to mysite.com, right?

 

I don't understand why the second RewriteRule also has to be in the /blog/ folder instead of the site root. I've tried putting it in the root folder as

 

RewriteRule ^blog/archives/(.*)/(.*)/(.*)/(.*)$ blog/archives/view.php?date=$1-$2-$3&keyword=$4

 

--basically, it's the same rule, but with the addition of the /blog/ folder. But it doesn't work--I just get my error page, and when I echo out the referer, I get the fake blog/archives/2005/02/10/puppy URL and not the URL I want it to go to. Yet as soon as I move it to the /blog/ folder, it suddenly works for me. Is there a reason for that? I'm confused.

 

Granted, it works, so I shouldn't complain, but I hate not understanding things. Does anyone understand rewrite rules enough to know why I can't move the second rule, at least, to the root folder? Does it have something to do with the fact that I've stopped using the subdomain and am redirecting traffic back to the regular mysite.com/blog/ ?

 

Any help is much appreciated. I'm just trying to understand what's going on.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...