View Full Version : rewrite with redirect

08-10-2011, 01:10 AM

recently i have change the location of my web pages and because of google indexing i need to redirect the old ones to their new locations. Example: google is indexing this www.domain.com/pages/subpages/page.php but its new location is www.domain.com/pages/page.php. So i have added to my .htaccess simple redirect query and it's working. But I want also to rewrite the URL (remove "pages/" and php extension) so it will look like www.domain.com/page.

It should work like this: Someone clicks on my old indexed webpage (www.domain.com/pages/subpages/page.php), it will redirect him to this link -> www.domain.com/page, which should open a www.domain.com/pages/page.php , but leave in the user address bar the rewriten page (www.domain.com/page).

So i have made this .htaccess code

Redirect 301 /pages/subpages/page.php http://www.domain.com/page
RewriteEngine On
RewriteRule ^page/?$ /pages/page.php

It works almost correctly. When I open the page by www.domain.com/page it opens me a www.domain.com/pages/page.php and leave the rewriten page in address bar (that's what i have wanted to). But when I open it by the old link www.domain.com/pages/subpages/page.php , the redirect works but it leaves in the user's address bar the full page www.domain.com/pages/page.php, not the rewriten one (http://www.domain.com/page). It looks like the redirect and rewrite doesn't work correctly together. How to solve this?

mod_rewrite is allowed in my Apache. I tried to add line (I don't know what it really does)

Options +FollowSymLinks
but it gave me an 500 internal error.

I hope it's comprehensible.

08-10-2011, 09:54 AM
Are you sure your browser hasn't cached a re-direct? The Redirect directive will simply send a response back to the client which essentially says "this resource now exists here, if you still want it, request that instead". At that point a secondary request to the server is made for the new address. Therefore if the new address works when you request it, the same result should be seen when requesting the address that is redirected.

NB. It's always a good idea to test these things with 302's, so you know your browser won't cache it. Only change to 301 when you're happy it's working.

08-10-2011, 12:55 PM
I have cleared browser cache and it's working even with 301.


08-11-2011, 12:20 AM
One more question - How do I write regexp for substring at the very end of an URL?

I want to do redirect JUST for http://www.domain.com/pages , but not http://www.domain.com/pages/page1

08-11-2011, 09:40 AM
That's what your current regex does. The dollar at the end of the pattern is what's referred to as an anchor, it means match the end of the string.