Forum Moderators: phranque

Message Too Old, No Replies

Rewrite does nothing

My 301 rewrite works, but my internal rewrite doesn't. What gives?

         

coruscate

4:15 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



I've been fighting with this for two days, and it's driving me nuts. My customer has a site built on a blog platform. His registrar allows .htaccess edits, but the site itself is hosted with a different company.

This is what I've added to the .htaccess file:

RewriteEngine on

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

RewriteRule ^/([^/]+)$ /2010/02/$1


The first rule is just a basic canonical URL forward. The second rewrite is supposed to rewrite a simple page URL (i.e. http://www.example.com/page.html) so it pulls the "static" blog pages the customer created last February (i.e. http://www.example.com/2010/02/page.html). I need it to ignore any pages in subdirectories (i.e. http://www.example.com/2011/07/newpage.html) because the rest of the pages are actual blog posts.

The first rule works great. The second does absolutely nothing. What am I doing wrong?

Thanks in advance for the help!

g1smd

4:22 pm on Jul 21, 2011 (gmt 0)

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



Is the second rule meant to accept URL requests and bounce the user to a different URL (a redirect), or is it supposed to rewrite the request pointer to find the content at a non-default location inside the server (a rewrite)?

The second rule should have at least the [L] flag. If it is supposed to be a redirect then the target should include the domain name and the rule would also need [R=301,L] flags.

For a rewrite, the RegEx pattern should match the URL request the user makes when clicking the links on the pages of the site and the target should be the physical internal location inside the server where that content resides.

The first step is to link to the URL you want users to see and use. URLs are defined in links. Rewriterule cannot change URLs nor can it change the links on your pages.

coruscate

4:31 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



Thanks for the quick response :)

The second rule is supposed to rewrite the request pointer to find the content in a different location.

The link is http://www.example.com/page.html

I want it to pull content from http://www.example.com/2010/02/page.html

I think the problem might be that the files are hosted on a different server than the .htaccess file. Maybe.

g1smd

4:46 pm on Jul 21, 2011 (gmt 0)

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



"URLs" are a reference system used out there on the web and "filepaths" are a reference system used here inside the server.

If you employ an internal rewrite the target must be somewhere inside your own server's filesystem.

If you redirect the request, the user will see the redirected-to URL appear in their browser address bar.

coruscate

4:59 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



Okay, so it sounds like the problem is probably related to the site being split across two servers and linked with a CName. I can't use rewrites unless the files reside locally. What I need to do is move the .htaccess to the file host instead of the registrar server. Good to know :)

Thank you for your help!

[edited by: coruscate at 5:05 pm (utc) on Jul 21, 2011]

g1smd

5:04 pm on Jul 21, 2011 (gmt 0)

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



Yes. That's all good.

You need to make sure that after the root file URL request is rewritten, the new pointer doesn't match the rule that is looking to rewrite requests into your dynamic blog system.