Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    rewrite with redirect

    hello,

    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
    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)
    Code:
    Options +FollowSymLinks
    but it gave me an 500 internal error.

    I hope it's comprehensible.
    Last edited by Radovan; 08-10-2011 at 12:14 AM.

  • #2
    New Coder
    Join Date
    Jul 2011
    Posts
    67
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have cleared browser cache and it's working even with 301.

    Thanks

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Jul 2011
    Posts
    67
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •