htaccess redirect code – redirect old page to new page

Home Forums BulletProof Security Free htaccess redirect code – redirect old page to new page

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #6418
    Adendum
    Participant

    Hi,

    I have only just set up BPS, so a noob!

    I needed to add some custom code and a forum search isolated where that should go (custom code bottom) so I copied the redirect section from my old .htaccess (which was working) into the bottom section, generated the new .htaccess and activated. But a couple of tests gave me the default 404 page, which I wasn’t expecting.

    So after reading several more posts from this forum and the http://forum.ait-pro.com/forums/topic/htaccess-redirect-code-where-do-i-add-redirect-htaccess-code/ authors tips and tricks I now have a set of redirects that are in a bit of a mess and not working.

    So my question is what is the correct way to handle these….

    [a] RedirectMatch 301 ^html/old-page.php$ /new-page/
    [b] RewriteRule ^html/old-page.php$ /new-page/ [R=301,NC,L]

    or is there a [c] ?

    Oh and in my old .htaccess I had the following..

    # Begin old site rewrites
    RewriteEngine on
    RewriteBase /
    - here were all the 50 or so redirects
    # End old site rewrites

    So in the custom section should I also use “RewriteEngine on” and “RewriteBase /” ?

    Yup…I’m totally confused!!

    #6421
    AITpro Admin
    Keymaster

    Post the links/URLs that you want to redirect and where you want them to redirect to.

    Example:

    I want to redirect this URL forum.ait-pro.com/some-post-or-page/ to this URL forum.ait-pro.com/a-different-post-or-page/

    #6422
    AITpro Admin
    Keymaster

    You do not need to post all 50 of them.  Just post a few of them.

    #6423
    Adendum
    Participant

    Thanks…the old site was a flat html site and the new site is a WP site and the following are some of the redirects from the OLD .htaccess file.

    # Begin old site rewrites
    RewriteEngine on
    RewriteBase /
    RewriteRule html/about.php /about-us/ [R=301,NC,L]
    RewriteRule html/anti-sweep.php /category/products/anti-sweep-system/ [R=301,NC,L]
    RewriteRule html/bottle_safers.php /category/products/bottle-safers/ [R=301,NC,L]
    ......
    RewriteRule pdf/Environmental%20Policy.pdf /about-us/environmental-policy/ [R=301,NC,L]
    RewriteRule pdf/FORTKNOX4.pdf /fortknox-safers/ [R=301,NC,L]
    RewriteRule pdf/DUAL%20SAFERS%201.pdf /dual-safers/ [R=301,NC,L]
    ......
    # End old site rewrites
    
    

    The new .htaccess file has had numerous different attempts and the following is what I have in there at the moment…

    # CUSTOM CODE BOTTOM
    # Begin old site rewrites - attempt 7
    RedirectMatch 301 ^html/about.php$ /about-us/
    RedirectMatch 301 ^html/anti-sweep.php$ /category/products/anti-sweep-system/
    RedirectMatch 301 ^html/bottle_safers.php$ /category/products/bottle-safers/
    .......
    RedirectMatch 301 ^pdf/Environmental%20Policy.pdf /about-us/environmental-policy/
    RedirectMatch 301 ^pdf/FORTKNOX4.pdf /fortknox-safers/
    RedirectMatch 301 ^pdf/DUAL%20SAFERS%201.pdf /dual-safers/
    .......
    # End old site rewrites
    
    #6424
    AITpro Admin
    Keymaster

    I assume the html and pdf references in your code are folders correct?

    /html
    /pdf

    Some problems that I see right away are these.  If the pdf folder is a folder then start with a foward slash, end with a $ sign, you need to point to the actual file if you are redirecting that link/file to another folder and your redirect “to” link/URL needs to have the full URL path (HTTP etc).

    RedirectMatch 301 ^/pdf/FORTKNOX4.pdf$ >http://example.com/fortknox-safers/FORTKNOX4.pdf
    #6430
    AITpro Admin
    Keymaster

    Looking at this example something does not quite make sense to me.  If you have created a WordPress About Us page then you would just copy the content of the old about.php file into your new WordPress About Us page.  If you are trying to make sure that old links to your site do not get 404 errors then…

    RedirectMatch 301 ^html/about.php$ /about-us/
    ...you would do this...
    RedirectMatch 301 ^/html/about.php$ http://example.com/about-us/
    #6432
    Adendum
    Participant

    Yes you are correct – pdf and html were folders on the old site.

    And yes you are correct again – the old html/about.php needed to redirect to the new /about-us/ so any old bookmarks/links didn’t give a 404 error.

    More to the point – a big thank you for such speedy and accurate advice. Having edited my custom code to ensure the leading carat and slash before the folder and the trailing $ after plus the FULL URL made all the difference. Everything is working again now.

    I confess I don’t understand the difference between my old .htaccess coding and the new. It may not be worth explaining the difference in this post but I am intrigued to understand why the old .htaccess did work perfectly and had done for about a year since the site changed to WP but didn’t when I pasted the bulk of it into the custom code section. Of course I can see that “RewriteRule” is entirely different to “RedirectMatch 301” and my new .htaccess is considerably bigger and better, thanks to you! 🙂

    I’ll be adding this plugin to a few other sites shortly.

    I much appreciated your help.

    #6435
    AITpro Admin
    Keymaster

    When using RedirectMatch you want to use the most accurate/exact conditions as possible for the primary reason that you would not want to match something else by accident, which would cause either errors or worse redirect looping – redirects that redirect back on themselves in an endless loop.  You also want to use the caret ^ and $ sign to say start looking here ^ and do not look past the $ for a matching condition. Since you want to match starting from the folder /html then that is why you would add the slash.  Since you want to stop looking at a certain point you add the $ sign.

    Valid RedirectMatch code follows this format:  portion of matching URL condition to Full URL were the redirect is pointed too.  In some cases you would want to use RewriteRule instead of RedirectMatch, but in most cases where you want to redirect URLs – RedirectMatch is the optimum directive to use/choose.

    For additional explanations and other examples see this Forum Topic link below.

    http://forum.ait-pro.com/forums/topic/htaccess-redirect-code-where-do-i-add-redirect-htaccess-code/

    Let’s say you have a bunch of conditions that you are matching/filtering then RewriteRule is the optimum directive to use/choose.

    # BuddyPress Anti-Spam Registration
    RewriteCond %{REQUEST_METHOD} ^POST
    # only match the register page URI
    RewriteCond %{REQUEST_URI} ^/register/$
    # domain referer is better than IP - allows for subdomains
    RewriteCond %{HTTP_REFERER} !^.*ait-pro.com.* [OR]
    # Blank or single hyphen user-agent spoofers or curl
    RewriteCond %{HTTP_USER_AGENT} ^(|-?|curl)$ [NC,OR]
    # HTTP request header
    RewriteCond %{THE_REQUEST} HTTP/1\.0$
    RewriteRule ^(.*)$ /spam-prevention [R=301,L]
    #15991
    Bro Ignatius Mary
    Participant

    [Topic has been merged into this relevant Topic]

    I have reorganized a wordpress. Some page urls have changed. I used htaccess to redirect the old url to the new one. If I have the secure htaccess set by bulletproof I get a 404 message within wordpress. It appears that the only way to use the redirects is to have  default htaccess instead of a secure one. Is there a way around this so that I can use the redirects and they work?

    #15995
    AITpro Admin
    Keymaster

    What does “reorganized a wordpress” mean?  Did you move or migrate your WordPress site?
    “Some page urls have changed”.  How have they changed?  Did you change your permalinks?  What are the old page URL’s and what are the new page URL’s?
    “I used htaccess to redirect the old url to the new one”.  Post the redirect .htaccess code that you used and where you used it, which files you used it in, etc.

    #15999
    Bro Ignatius Mary
    Participant

    I mean that a oldpage was deleted and another newpage created with the same information. I did a redirect in the htaccess to automatically redirect oldpage to newpage, but Bullet Proof will not allow that and instead produces a 404 file not found. If I do not use the secure htaccess provided by BP, then the following RewriteRule works.

    here is the RewriteRule in my htaccess:

    RewriteRule ^warfare/library/?page_id=193$ http://saint-mike.org/warfare/library/spiritual-warfare-prayer-catalog [R,NC,L]

    page_id=193 no longer exists. the shortlink to the new page is: http://saint-mike.org/warfare/library/?p=817

    Since the page_id=193 url is still out there is Internet land, I need to make a redirect.

    #16013
    AITpro Admin
    Keymaster

    Instead of trying to do a Rewrite it is better to do a Redirect for this. Also you may need to add a forward slash in front of the “warfare” directory/folder.

    RedirectMatch 301 ^warfare/library/(.*)page_id=193$ http://saint-mike.org/warfare/library/spiritual-warfare-prayer-catalog/
    RedirectMatch 301 ^/warfare/library/(.*)page_id=193$ http://saint-mike.org/warfare/library/spiritual-warfare-prayer-catalog/
    

    or

    RedirectMatch 301 ^warfare/library/(.*)page_id=193$ http://saint-mike.org/warfare/library/?p=817
    RedirectMatch 301 ^/warfare/library/(.*)page_id=193$ http://saint-mike.org/warfare/library/?p=817
    
    #24698
    Adendum
    Participant

    I’ve tried several variations and so far nothing is working – but it could be I have the code in the wrong place too? I have quite a few 404’s showing up in Google Webmaster that I need to redirect. There was an old version of the site written in PHP but it is now a WordPress site. The 404 errors are coming in like this and so on:-

    http://www.domain.net/classifieds/?do=contactus
    http://www.domain.net/classifieds/?do=detailpage&cat=3&id=82
    http://www.domain.net/classifieds/?do=detailpage&cat=3&id=16&page=2

    I want to redirect these to http://domain.net/ads/ and I don’t need to worry about any of the rest (?do= etc) as none of the old ads are available so I just want visitors to get the overall category page. And I’ve tried several versions using RedirectMatch and several using RewriteRule but nothing seems to get through. I’m putting the code into the Custom Code WP Rewrite Loop Start section.

    #24703
    AITpro Admin
    Keymaster

    My interpretation of what you want is basically you want to end up with redirected URL’s that point to this: http://www.domain.net/classifieds/ without the Query String on the URL correct? Or is there another folder involved called /ads/?

    If what you want is to strip the Query String from the URL|URI then this is the code you want:

    # Strips the do=contactus and do=detailpage Query Strings from the URL|URI
    RewriteCond %{QUERY_STRING} ^do=contactus$ [NC,OR]
    RewriteCond %{QUERY_STRING} ^do=detailpage(.*)$ [NC]
    RewriteRule ^(.*)$ $1? [R=301,L]

    Another Example of replacing Query Strings just for fun:

    # Replaces Query Strings: do=contactus and do=detailpage with Query string: ?keywords=[keyword]
    RewriteCond %{QUERY_STRING} ^do=contactus$ [NC,OR]
    RewriteCond %{QUERY_STRING} ^do=detailpage(.*)$ [NC]
    RewriteRule ^(.*)$ $1?keywords=[keyword] [R=301,L]

    To add this custom code to BPS Root Custom Code these are the steps:

    1.  Go to the BPS htaccess File Editor page, click on the Your Current Root htaccess File tab, scroll down in your Root .htaccess file code until you see this .htaccess code below (or similar code – may look slightly different than the code below).

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

    2.  Copy your # WP REWRITE LOOP START code to this BPS Root Custom Code text box:  CUSTOM CODE WP REWRITE LOOP START
    3.  After you have copied your WP Rewrite Loop Start .htaccess code to the Custom Code text box add your particular custom  htaccess redirect code.  Your code should look something like the example below.
    4.  Click the Save Root Custom Code button.
    5. Go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    Example:

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # Strips the do=contactus and do=detailpage Query Strings from the URL|URI
    RewriteCond %{QUERY_STRING} ^do=contactus$ [NC,OR]
    RewriteCond %{QUERY_STRING} ^do=detailpage(.*)$ [NC]
    RewriteRule ^(.*)$ $1? [R=301,L]
    #24717
    Adendum
    Participant

    I don’t get why you have merged my last post with one of my much older posts….it makes reading the entire thread very confusing. The problem is that the old website had a different structure and all the adverts were created in the folder (url) of /classifieds/ and the new WordPress website uses a category of /ads/. The 404 errors that Google reports number in the hundreds but are all unique but all start with /classifieds/?do= My aim is to find a simple way to intercept all of these /classifieds/?do= requests and send them all to /ads/

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.