SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Location
    Sydney, Australia
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing 301 Redirects

    Hello,
    I've never had to do 301 Redirects before, so I'm playing around with them on a local site have set up. (Its a wordpress site running on MAMP).

    As I understand it, if I test redirecting /page1/ of the site to http://www.mysite/page2/, when I click on a link for page1 shouldn't it automatically redirect to page2? Because that's not happening.

    1. Does the 301 Redirect only work when clicked from Google?
    2. OR Is it not working because I'm trying to do it on a local site?
    3. OR is there some other reason its not working?

    My code in the .htaccess is:

    Code:
    Redirect 301 /page1/ http://mysite/page2/
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /mysite/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /mysite/index.php [L]
    </IfModule>
    
    # END WordPress
    I've also tried it using the wordpress redirection plugin, not working on that either.
    Please help, I'm really trying to get my head around these 301s. I know it will be fairly straightforward once I understand it!
    Thanks.

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

    Your answer is no, it should redirect to page2/ (page2's DirectoryIndex). Keep your directories and files straight!

    There is an additional problem, though, in that WP will hijack EVERY (except file or directory) request and send to index.php. To test your Redirect, turn mod_rewrite off (RewriteEngine off).

    Finally, while it doesn't matter (you're free to abuse your MAMP installation) ...

    [rant #4]
    The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T BE AN IDIOT! If you don't know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).
    [/rant 4]

    Sorry, that's a standard rant reserved for novice webmasters who have no clue what their code does.

    As for your numbered questions:

    1. Does the 301 Redirect only work when clicked from Google? - NO!
    2. OR Is it not working because I'm trying to do it on a local site? - NO (see above)!
    3. OR is there some other reason its not working? - YES (see above)!

    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Location
    Sydney, Australia
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey DK - I like your cut & paste rants!
    Thanks for answering. I realise you probably get these questions all the time.

    Based on your comments I've done the following
    1. fixed my redirect statement - see below
    2. removed the IfModule wrapper - but this broke my wordpress I believe because its using pretty permalinks. So I reverted back to the default permalinks and that got rid of that whole Wordpress block in the htaccess file. I'll deal with that later.

    Even with just my simple 301 redirct statement its still not working.

    My code in a completely empty htaccess file is:

    Code:
    Redirect 301 /?page_id=25 http://mylocalsitename/?page_id=29
    Does anything else need to be in the file?
    After saving the file and going to page 25 on the site, I should be redirected to page 29 right away - correct? That isn't happening. I tried restarting my MAMP, but that still didn't work.
    Peculiar also that the Redirection plugin isn't working, that doesn't even rely on htaccess.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Location
    Sydney, Australia
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to update this thread to say that the Wordpress plugin Simple 301 Redirects is workingon my local wordpress install, with no need to switch off pretty permalinks.

    BUT > I would really like to get my 301 Redirects working in .htaccess, so any further advice appreciated!

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

    Thanks! As the former Hosting Team Leader, I was repeating explanations so often that I created "shortcuts" to automated text (AutoHotKey is a fabulous tool!). I've since "retired" from staff but AutoHotKey is still a massive time saver!

    If removing the <IfModule> wrapper broke your website, it's obvious that mod_rewrite is not enabled so removing the code is appropriate (actually, I would have merely commented it out, found a new host and enabled it there).

    Did you remove the trailing /'s on your PAGE Redirect statements?

    OIC - you expected mod_alias to be able to examine the query string! Sorry, it doesn't! I believe you'll need the power of mod_rewrite to do that for you. Even the Apache Foundation says (http://httpd.apache.org/docs/2.2/mod/mod_alias.html) "mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite.' When you get a good host, you can use

    Code:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} page_id=25$
    RewriteRule index\.php index.php?page_id=29 [R=301,L]
    Without this peculiarity of mod_alias, you were quite correct that mod_alias is the preferred tool for simple redirections.

    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

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Location
    Sydney, Australia
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks dk - I might have to leave this for now and look into it again later. The MAMP install has mod_rewrite on by default. Not sure about mod_alias.
    I think this might be a MAMP issue though. Thanks again.

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

    No problem. FWIW, though, mod_alias is part of Apache's core so there's no enable/disable, it's just ON! It's mod_rewrite which needs to be specifically enabled (disabled by default).

    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


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
  •