Ok, so I've been banging my head against the wall with this for way too long now. Doesn't seem like this should be so damn difficult. All I want to do is simply take a nasty form our blogs and turn it into a nice friendly SEO url. For example, http://www.oursite.com/blog-post.php?post=2344 Instead, for better SEO value, we want the blog title in the URL. So I wrote a RewriteRule that does this: http://www.oursite.com/blog-post/the-actual-title-of-the-blog-post I've seen this on many blogs before, so I know it's definitely possible. I have the rewrite rule actually working. It's taking that URL and translating it into this: http://www.oursite.com/blog-post.php?title=the-actual-title-of-the-blog-post Our PHP script is then taking that title parameter and pulling the post. Works great. However, once the RewriteRule is processed all of the content on the page that is being loaded (images, css, ANYTHING) totally fails to load. The server log shows that the rule is actually rewriting all of the requests for those assets as well causing them to obviously not load. Why can I not seem to get it to just simply rewrite that main URL and leave the rest of the items on the page alone????!! Here is my .htaccess file configuration: RewriteEngine On RewriteBase /rewrite-test RewriteRule ^old\.php$ rewrite.php [R] RewriteRule ^person/(.*)/(.*)$ rewrite.php?name=$1&age=$2 Code (markup): I did a couple things here to confirm that mod_rewrite is working. It's successfully redircting old.php to rewrite.php. All good there, and of course the other rule is working, but it's just affecting everything on that page, which is absolutely awful. I tried adding a RewriteCond before that last rule to skip other assets, something like: RewriteCond %{REQUEST_URI} !(\.(gif¦jpg¦css)$¦^/rewrite\.php$) But that did absolutely nothing either. Please help! There has to be a way to get it to rewrite the URL but still be able to load the content on the page without any problems. Can;t believe how complicated such a simple task is.... -Jason
Your rewrite rule needs to direct whatever link you are supplying to: blog-post.php?post=2344 So the rewritten portion must contain the 2344 and that is transferred to the redirected portion: ^ what is to be redirected < a single space> blog-post.php?post=$1 So to put all this together your scripts must write out the friendly URL and then when they are clicked it redirects to the same old thing. It is a two step process. Here is a sample of one that works, I have changed my file name to +++ for security reasons: # rewites the ++++.php URLs using / note the $3 is for the pagination RewriteRule ^([0-9]+)/(.*)-([0-9]+)\.html$ ++++.php?cat_id=$1&page=$3 [L,NC] PHP: This particular one takes a link that looks like: http://mysite.com/7/Category-name-1.html PHP: and redirects it to the proper script, category (7) and page one of the pagination while making the whole thing look like a static html page.
Hi, well I actually posted the wrong rewrite rule there. I do have a rewrite rule written that does work at translating my blog-post urls properly. Here it is: RewriteRule ^blog-post/(.*)$ blog-post.php?title=$1 Code (markup): This takes my friendly URL of blog-post/title-here and makes it blog-post.php?title=title-here BUT the main problem is that the PHP page blog-post.php contains links to CSS, images, etc to display on the page. All of those links are relative URLs. For example, when I'm displaying an image on that page, the image tag is something like <img src"images/image.jpg"/>. All of the images, CSS, etc are failing to load when the rewrite takes effects becuase, looking at the rewrite logs, the rule is rewriting those relative URLs as well! Which totally breaks them and causes them not to load. I need a way to have it NOT do that and just simply rewrite the main url only and leave everything else alone. Is that possible? In my previous post, you see I tried a RewriteCond trying to skip any urls with images or css extensions, but that didn't work. any ideas?
Would be easier if I could see the site, so this is going to be a guess. I assume your images are being rewritten to http://www.oursite.com/blog-post/the-actual-title-of-the-blog-post/someimage.jpg in which case in the head section of the HTML add <base href="http://www.oursite.com/" /> I'm assuming your assets are relative to the page therefore the above should make them relative to the home page which would be the case in the original URL.
Simply adding the < base href= http:sitename" > to the first line of your <head> should solve the problem. See another post I made just a few days back for the exact syntax.
Thanks so much Tolra! That totally fixed the problem. I had messed with the RewriteBase and made sure the apache document root was working, but never thought of a html base tag! =D