SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Location
    UK
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wild Card Redirect not working properly

    Hi,

    I've built a website at a new domain for a client and want to completely remove the old site so Google isn't seeing any duplicate content and they will have a completely fresh start.

    In Cpanel on the old site I've setup a wild card redirect for the root to point to the new website which I thought would cover things so that any page on the old site would redirect to the new domain. However, sub pages found on Google aren't being redirected to the new site but just getting a 404 error which has baffled me.

    Not great on server config stuff so any advice would be appreciated. the old site was Wordpress in case that's relevant.

    Many thanks in advance

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Hi Arthur,


    Are you familiar with using mod_rewrite in an .htaccess file in your old site's web directory?


    You need to have the mod_rewrite module enabled in Apache. Once mod_rewrite is working you could redirect the old to new URLS like:


    Code:
    RewriteEngine ON
    RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301]
    RewriteRule ^(.*)$ http://mynewdomain.com/404.php?url=$1 [L]
    This rewriting script would need to be added to a .htaccess file located in the root directory of your old domain. If you don't know if your mod_rewrite is working then you can link to Dklynn's tutorial as there is an example there of how to test mod_rewrite.


    • Line 1 enables the rewrite engine
    • Line 2 maps all requests on the old site and redirects it to the new domain set with a permanent redirection flag (R=301) and the so the search engines will update the robots to go to the new domain.
    • Any pages that are linked to the old site will be set in the (.*) match. The pages will then be substituted in the $1 back reference variable and so as long as the pages are named the same thing the redirection will work.
    • The final rule is reached if a page is not found. The L flag tells mod_rewrite to stop processing rules. In this example the 404.php file would handle any page request that were not found. It passes a GET parameter 'url' equal to the $1 variable to your php page so the value of the not found page can be reported in the 404.php page (the php variable can be accessed via $_GET['url']).

    If this doesn't work then you can do a more comprehensive rewrite map to make explicit redirects. It is more complicated to set a rewrite map up and you need to have access to your http.conf or apache.conf file as this cannot be used in an .htaccess file.

    Hope this helps.

    Steve
    Last edited by ServerStorm; Nov 7, 2012 at 15:31.
    ictus==""

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Aw, Steve, really?

    Code:
    RewriteEngine ON
    RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301]
    # With EVERYTHING already redirected, the 404 will NEVER be seen let alone matched
    RewriteRule ^(.*)$ http://mynewdomain.com/404.php?url=$1 [L]
    Arthur,

    TO redirect EVERYTHING, simply use mod_alias' Redirect statement:

    Code:
    Redirect 301 / http://newdomain/
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dklynn View Post
    Aw, Steve, really?

    Code:
    RewriteEngine ON
    RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301]
    # With EVERYTHING already redirected, the 404 will NEVER be seen let alone matched
    RewriteRule ^(.*)$ http://mynewdomain.com/404.php?url=$1 [L]
    Arthur,

    TO redirect EVERYTHING, simply use mod_alias' Redirect statement:

    Code:
    Redirect 301 / http://newdomain/
    Regards,

    DK
    Yup I should have seen this.

    Do most hosts have mod_alias enabled? He currently manages his server using cpanel and I'm not sure that it could work the way you specify?

    Regards,
    Steve
    Last edited by ServerStorm; Nov 8, 2012 at 09:15.
    ictus==""

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    york
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feedback guys.

    I've edited the htaccess file to the above. The home page redirects but any URLs to sub directories or different pages generate a 404 missing page rather than redirecting to the home page on the new site which is waht I was hoping to achieve. Also I've uploaded a test.php page that has the phpinfo() call and it is showing a 404 on that one too. I'm baffled.

    RewriteEngine ON
    Redirect 301 / http://www.newdomain.co.uk/

    Any ideas? Many thanks in advance again.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Steve,

    mod_alias is part of Apache's core so it is always available.

    Arthur,

    Hmmm, another case of failed specificity!

    If you merely want to redirect everything from your old website to a specific page on the new website, try:[code]Redirect / http://newdomain/test.php[\code]i.e., name the page else mod_alias should retain the {REQUEST_URI} variable.

    If that fails, I fall back on mod_rewrite which would be:
    Code:
    RewriteEngine on
    RewriteRule .? http://newdomain/test.php [R=301,L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dklynn View Post
    Steve,

    mod_alias is part of Apache's core so it is always available.
    ...
    Thanks
    ictus==""

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    york
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all- the following code in .htaccess has all indexed pages on the old site redirecting to the new home page
    RewriteEngine on
    RewriteRule .? http://www.newdomain.com [R=301,L]


Bookmarks

Posting Permissions

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