htaccess Redirect Code – Where Do I Add Redirect htaccess Code

Home Forums BulletProof Security Pro htaccess Redirect Code – Where Do I Add Redirect htaccess Code

Viewing 15 posts - 1 through 15 (of 32 total)
  • Author
    Posts
  • #417
    AITpro Admin
    Keymaster

    I get asked this question a lot so I thought I would put together a basic guide to adding htaccess redirect code and where to add it in your root htaccess file.  htaccess redirects can be used to redirect old web pages to new web pages, old posts to new posts, old directories to new directories, and vice versa and of course to get rid of any 404 errors.  Basically you redirect anything to anything very quickly and easily with htaccess redirect code.  Since Redirect code can be put anywhere in your root .htaccess file then usually the best place for it is at the end or bottom of your root .htaccess file.

    1. If you have BPS or BPS Pro installed then add your redirect .htaccess code to this BPS Custom Code text box:  CUSTOM CODE BOTTOM HOTLINKING/FORBID COMMENT SPAMMERS/BLOCK BOTS/BLOCK IP/REDIRECT CODE
    2. Click the Save Root Custom Code button.
    3. Go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    IMPORTANT NOTE:  If you have Go Daddy hosting then click this link http://forum.ait-pro.com/forums/topic/go-daddy-redirect-htaccess-code-no-longer-working/  for examples of using RedirectMatch to redirect URLS on Go Daddy.

    So let’s start out with some simple examples and later I will add some more complex examples to this Topic.
    Let’s say you created a post called – my old post and you now want to redirect this post to a post called – my new post.
    The full URL to this old post is example.com/old-category/my-old-post
    The full URL to the new post is example.com/new-category/my-new-post

    I want to point out a couple of important things first. You should always use the ^ and $ code characters which mean the start and end of a URI|URL path that you want to redirect from (see examples below).  A back reference $1 (see examples below) will look for the $ in the first part of the RedirecMatch URL and is basically something like a placeholder or reference point or an “URL splicer”.  You can of course have many back references ($1, $2, $3, etc) in more complex htaccess code, but for simple RedirecMatch coding you will only have one back reference.  The reason you should always try to use a ^ starting point and a $ ending point is you could have similar URL’s that also would get redirected that you did not want to get redirected.  See the second example below.

    NOTE:  The “+” sign can also be a place holder for a back reference – Click to go to that section in this Forum Topic –  Back Referencing the Plus Sign.

    This is the htaccess code that you would use to permanently redirect the “old post” to the “new post” and to the new category as well.

    RedirectMatch 301 ^/old-category/my-old-post$ http://www.example.com/new-category/my-new-post$1

    Let’s say you did not add a ^ starting point and a $ ending point for this redirect below.

    RedirectMatch 301 /aitpro-blog/category/misc-projects/wordpress http://www.ait-pro.com/aitpro-blog/4349/misc-projects/wordpress-tips-tricks-fixes

    What would happen is that this redirect code would redirect all 3 of these posts below to the “wordpress-tips-tricks-fixes” post since you have not added a starting point and and ending point for the URL.

    /aitpro-blog/category/misc-projects/wordpress
    /aitpro-blog/category/misc-projects/wordpress-blah1
    /aitpro-blog/category/misc-projects/wordpress-blah2

    By adding your ^ starting point and the $ ending point you are saying STOP at the $ sign and do not look any further since this is the end point of the URL you want to redirect.

    This redirect below will only redirect – /aitpro-blog/category/misc-projects/wordpress to http://www.ait-pro.com/aitpro-blog/4349/misc-projects/wordpress-tips-tricks-fixes/ and the blah1 and blah2 examples above would not be redirected.

    RedirectMatch 301 ^/aitpro-blog/category/misc-projects/wordpress$ http://www.ait-pro.com/aitpro-blog/4349/misc-projects/wordpress-tips-tricks-fixes/$1

    Here is quick example of using the $1 back reference in a way that makes it very clear about how it works.  Notice in this example that the back reference is not at the end of the RewriteRule and is actually at the beginning.  There are times when using the $1 back reference should not be used because it will create another redirect that will be incorrect and generate a 404 error.  When NOT to use the $1 back reference.

    Stripping, adding or replacing Query Strings & redirects:

    Important Notes:  If you have a WordPress site then this code needs to be inside of the WordPress Rewrite Loop. See this forum topic for how to add this code in BPS Custom Code: http://forum.ait-pro.com/forums/topic/i-had-a-working-set-of-redirects-but-not-now/#post-24703  Query Strings are cached by the Google Chrome browser and probaby all other browsers so if you are changing things with your Query String htaccess code then you should clear your browser cache in-between each change you make to ensure you are seeing current htaccess code processing and not previous/past htaccess code processing.

    This code says that if a Request for this URL|URI /blah/testing-appending-query-string/(.*) – (means match anything) is made then Rewrite this Request and the back reference will start from where the $ sign ends in the first part of the RewriteRule – you end up with this URL – /blah/testing-appending-query-string/?keywords=[keyword] . The URL|URI and Query String are “spliced”/added together by using $1.

    RewriteEngine On
    RewriteBase /blah/
    RewriteCond %{REQUEST_URI} ^/blah/testing-appending-query-string/(.*) [NC]
    RewriteRule ^(.*)$ $1?keywords=[keyword] [R=302]

    Strips the do=contactus and do=detailpage Query Strings from the URL|URI:

    # 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]

    Replaces Query Strings: do=contactus and do=detailpage with Query string: ?keywords=[keyword]:

    # 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]

    Strips Query String: action=register on the wp-login.php page so that the Registration Form will not load

    ## Prevents the WP Registration Form from loading
    RewriteCond %{REQUEST_URI} ^.*/wp-login.php [NC]
    RewriteCond %{QUERY_STRING} ^action=register(.*) [NC]
    RewriteRule ^(.*)$ $1? [R=301,L]

    Strips all Query Strings that start with do= ONLY if the Request URI is classifieds/ and redirects to the /ads/ URI:  http://www.example.com/classifieds/?do=detailpage&cat=3&id=14 to http://www.example.com/ads/

    # Redirects ONLY if the URI is classifieds/ and Strips the do= Query Strings
    # from the destination /ads/ URL|URI
    RewriteCond %{QUERY_STRING} ^do=(.*)$ [NC]
    RewriteRule ^classifieds/$ http://www.example.com/ads/$1? [R=301,L]

    Individual explicit and dynamic redirect rules based on Query String matches and stripping Query Strings from the destination URL|URI.
    Will redirect these URL’s and Query Strings:

    http://www.example.com/index.php?option=com_k2&view=item&id=7377
    http://www.example.com/index.php?option=com_k2&view=item&id=7378
    To these destination URL’s|URI’s & strip the Query Strings:
    http://www.example.com/artigos/7377/
    http://www.example.com/artigos/7378/

    # Individual explicit redirect rules based on exact matching URI|Query String parameters
    # $1? Strip the option=com_k2&view=item&id=7377 Query String from the destination URI
    RewriteCond %{QUERY_STRING} ^option=com_k2&view=item&id=7377$ [NC]
    RewriteRule ^(.*)$ /artigos/7377/$1? [R=301,L]
    RewriteCond %{QUERY_STRING} ^option=com_k2&view=item&id=7378$ [NC]
    RewriteRule ^(.*)$ /artigos/7378/$1? [R=301,L]
    
    # Dynamically redirect all matching Query Strings to equivalent URI
    # %2 match and redirect to equivalent 4 digit number URI
    # $1? Strip the option=com_k2&view=item&id= portion of the Query String from the destination URI
    RewriteCond %{QUERY_STRING} ^(option=com_k2&view=item&id+)=([0-9]{4}+)$ [NC]
    RewriteRule ^(.*)$ /artigos/%2/$1? [R=301,L]

    Ok for now here are some Redirects that are used on the AITpro Blog site to correct 404 errors that were showing up in Google Webmaster Tools.

    # Redirect 404 errors that show up in Google Webmaster Tools
    RedirectMatch 301 ^/aitpro-blog/misc-projects/funny-stuff/$ http://www.ait-pro.com/aitpro-blog/category/misc-projects/$1
    RedirectMatch 301 ^/aitpro-blog/category/misc-projects/wordpress$ http://www.ait-pro.com/aitpro-blog/4349/misc-projects/wordpress-tips-tricks-fixes/$1
    RedirectMatch 301 ^/aitpro-blog/website-metrics-posting-form/$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-posting-form/activation-key-verification/$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/bps-pro-php.ini$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/modify-bps-htaccess-file$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/custom-403$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/average-wordpress$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/bulletproof-security-pro-php$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/as3$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/apache-fastcgi$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/bullet$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/free-flash$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/translate-wordpress$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/403$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/bps-pro-php$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/add-a-banner$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/wordpress-website-under$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/translate-bullet$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/login$ http://www.ait-pro.com/aitpro-blog/login/
    RedirectMatch 301 ^/aitpro-blog/tag/best-custom$ http://www.ait-pro.com/aitpro-blog/
    RedirectMatch 301 ^/aitpro-blog/tag/crt-television-history\.\.$ http://www.ait-pro.com/aitpro-blog/tag/crt-television-history-and-facts/
    RedirectMatch 301 ^/aitpro-blog/tag/electronic-computer-his\.\.$ http://www.ait-pro.com/aitpro-blog/tag/electronic-computer-history-and-facts/

     

    # Redirect old website security testing posts that were generating 404 errors in Google Webmaster Tools

    # Redirects a URL starting out with numbers 0-9 and this is a range {2,5} which means match a range of 2 to 5 numbers
    # example 12, 201, 4352, 10300 would all match the range, but a single number like 4 or numbers over 5 number characters like 209467 (6 number characters) would not match the range.
    # this URL would be redirected - /45/website-security-842/ to /wordpress-testing-website/category/website-security-testing/
    RedirectMatch 301 ^/wordpress-testing-website/[0-9]{2,5}/website-security-[0-9]{2,5}/$ http://www.ait-pro.com/wordpress-testing-website/category/website-security-testing/
    # this URL would be redirected - /678/series-4/ to /wordpress-testing-website/category/series-testing/
    RedirectMatch 301 ^/wordpress-testing-website/[0-9]{2,5}/series-[0-9]{1,5}/$ http://www.ait-pro.com/wordpress-testing-website/category/series-testing/
    RedirectMatch 301 ^/wordpress-testing-website/wordpress-testing-website/author/admin/page/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/category/website-security-testing/page/(.*)$ http://www.ait-pro.com/wordpress-testing-website/category/website-security-testing/
    RedirectMatch 301 ^/wordpress-testing-website/category/series-testing/page/(.*)$ http://www.ait-pro.com/wordpress-testing-website/category/series-testing/

    When NOT to use the $1 back reference in redirect code

    If the $1 back reference is used to redirect this old post URL that was deleted from the aitpro-blog site…

    http://www.ait-pro.com/aitpro-blog/wordpress-tips-tricks-fixes/bulletproof-htaccess-file-code-wordpress-bulletproof-htaccess-code/

    …to a new category on the blog site then a 404 error will happen because the $1 back reference is “splicing” the URL together and redirecting to a non-existent URL.

    RedirectMatch 301 ^/aitpro-blog/wordpress-tips-tricks-fixes/bulletproof-htaccess(.*)$ http://www.ait-pro.com/aitpro-blog/category/misc-projects/wordpress-tips-tricks-fixes/$1

    The result of using the $1 back reference in the above redirect code is that you now end up with this “spliced” URL that will of course generate a 404 because this URL does not really exist.  Notice how the yellow hightlighted part of the new redirected URL is starting from the end of this part of the RedirectMatch code – /bulletproof-htaccess(.*) and then creates a new “spliced” URL starting from the end of the (.*) which is being back referenced by $1 and the rest or URL shown above in Blue highlight is now spliced / added to the new non-existent URL.

    http://www.ait-pro.com/aitpro-blog/category/misc-projects/wordpress-tips-tricks-fixes/-file-code-wordpress-bulletproof-htaccess-code/

    The correct way to redirect the old post URL:  If the $1 back reference is not used…

    RedirectMatch 301 ^/aitpro-blog/wordpress-tips-tricks-fixes/bulletproof-htaccess(.*)$ http://www.ait-pro.com/aitpro-blog/category/misc-projects/wordpress-tips-tricks-fixes/

    …Then the old post URL that was deleted from the aitpro-blog site will be redirected to the correct new category – category/misc-projects/wordpress-tips-tricks-fixes/ – so that a 404 error will not be generated.

    Other Examples:

    RedirectMatch 301 ^/wordpress-testing-website/68/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/2909/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/2911/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/1/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/97/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/93/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/Home$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/menu-(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/229/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/1226/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/87/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/104/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/55/(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/about/menu-(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/comment-page-1/Home(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/tag/beef/YAPB(.*)$ http://www.ait-pro.com/wordpress-testing-website/
    RedirectMatch 301 ^/wordpress-testing-website/tag/tim-thumb/TimThumb(.*)$ http://www.ait-pro.com/wordpress-testing-website/

    Back Referencing the “+” / Plus Sign

    Back Referencing the “+” / Plus sign works the same as back referencing a $ / Dollar sign in that you are “splicing” URL’s using a placeholder and a back reference $1, $2, $3, etc.  This is a much more sophisticated example of .htaccess redirect coding than the examples above.  There are assumed conditions in htaccess code and those assumed conditions are “if” and “and” unless you specify otherwise by using .htaccess flags such as [OR] which is exactly what it looks like and means “or” instead of “and”.  Breaking down the code below it translates to something like this:  if the first condition is matched up to the first back reference of $1 then redirect that URL.  if the second condition is matched up to the second back reference $2 then redirect that URL, etc etc etc.

    The Regex for this bit of code ([^/]+) means look at this group – a group is enclosed in round brackets ( and ) (aka parenthesis singular/parentheses plural).  The square brackets with the caret/circumflex character [^/] means match any single character that is not in the set (a set is everything inside of [ ] square brackets).  So in this case it is saying match anything, but the forward slash / which would mean you would be in another level of the URL.  /first-level/second-level/etc.  If you are in the first level of the URL then redirect or rewrite it, if you are in the second level or the URL then redirect or rewrite it, etc etc etc.  And since the caret/circumflex is used inside of the set it has a different meaning then when used at the beginning of this code below.  Outside of a set the caret/circumflex means the start of a condition and the $ sign means the end of a condition.

    This example below shows how to redirect old HTML pages to a new WordPress site and have those redirects point to Posts.

    Source:  http://stackoverflow.com/questions/11926237/redirecting-old-html-page-to-new-without-html-extension-page

    RedirectMatch 301 ^/([^/]+)/([^/.]+)\.html$ /$1/$2/ 
    RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/ 
    
    Or if you need to limit it by hosts, you can use mod_rewrite: 
    
    RewriteCond %{HTTP_HOST} sitename.com [NC] 
    RewriteRule ^([^/]+)/([^/.]+)\.html$ /$1/$2/ [R=301,L] 
    
    RewriteCond %{HTTP_HOST} sitename.com [NC] 
    RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/ [R=301,L]

    Simpler Example: Go Daddy subdomain site Rewrite|Redirect http to https
    We recently made this forum site HTTPS/SSL and ran into a problem with how this particular Request URI code: %{REQUEST_URI} was being interpreted by Go Daddy during the Rewrite process from http to https.

    Problem Scenario:
    Old http links such as this URL: http://forum.ait-pro.com/forums/topic/plugin-conflicts-actively-blocked-plugins-plugin-compatibility/ were redirecting incorrectly to this URL: https://forum.ait-pro.com/forum/forums/topic/plugin-conflicts-actively-blocked-plugins-plugin-compatibility/ The additional /forum/ path in the URI was coming from this code: %{REQUEST_URI} during the Rewrite process from http to https. Why our host does that I have no idea except for that our host requires adding the folder name of a subdomain in any redirect code that we use. So obviously Request URIs are interpreted differently on our particular host.

    Solution|Explanation:
    We needed to add an additional block of Rewrite code for rewriting http to https to handle the Problem Scenario described above. The + sign is a back reference for the group (/forum/forums+), which means back reference (placeholder/splice) this: /forum/forums to this RewriteRule RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]. The /$1 is the back reference referencing the + sign in the Request URI condition. The end result is the additional /forum/ path in the URI is removed from the final destination URL. All other URL’s that do not match the Request URI condition of: /forum/forums will be processed normally by the next block of code that Rewrites http to https.

    Subdomain site (this forum site): https://forum.ait-pro.com/
    Folder where this forum site is installed in our hosting root account folder: /forum/
    Base URL for BuddyPress/bbPress: /forums

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on
    RewriteCond %{SERVER_PORT} ^80
    RewriteCond %{REQUEST_URI} ^(/forum/forums+)/.*$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteCond %{SERVER_PORT} ^80
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    RewriteRule ^index\.php$ - [L]
    #24917
    jenni101
    Participant

    Your advice would be appreciated regarding redirects for content that is for logged in members only – as the google bot sees it as a 404 error! I now have 441 of these errors, but as the content is real (just protected) I’m not sure what to do. Is there a way to tell Google that it’s private? We still have some pages that are public but most pages, posts, uploaded images etc are not.

    many thanks.

    Update for anyone interested: since digging a little deeper in Google Search Console (ex webmaster tools) it seems that most of the 404 error were from links from an old html sitemap I’d submitted: attachment-sitemap.xml which i no longer use. So I’ve fixed it by resubmitting my correct html sitemaps and I’m hoping that the bots will now realise the old one doesn’t exist.

    #28555
    DKK
    Participant

    I am having an issue with important redirect rules which I need to set-up. Well, “may have an issue”, as I am unsure how the syntax should be, and thus don’t want to crash my site a dozen times trying to get it right.

    Presently I have redirect rules in my root .htaccess that look like…and they all work without a hitch.

    rewriterule ^old-version-page/ https://web.site/path/to/new-page-version/ [R=301,L]
    rewriterule ^another/old-example/page/ https://web.site/path/for_some-other/new-page/ [R=301,L]

    But the ones which I have to configure now have individual query strings. Examples:

    old-site.com/?p=164
    old-site.com/?p=6320
    old-site.com/?p=2357

    And ‘no’, I cannot use their alt. URLs. Each old page number would be going to a unique new page (which have different page numbers; and ‘yes’, I know that I need to check for numerical overlaps and correct such issues first). Now my problem is concerning what syntax to use for the redirects, as I know that certain characters (ex. ‘?’) must be escaped or substituted for everything to go right (rather than everything turning dead-white per se). I saw how to set up redirects that involve groups of query strings (ex. redirecting all pages with “?p=” to wherever), but not how to do so for specific queries (unless I’ve missed it somewhere).

    Thanks in advance.

    #28557
    AITpro Admin
    Keymaster

    Yep, Query Strings are tricky due to the way all Browsers are designed to interpret/handle/process Query Strings, which start with the question mark to signify processing/handling of a Query String instead of a Browser seeing the question mark as a literal character in a URL|URI.  So the htaccess code that you want to use is this example code below:

    Strips all Query Strings that start with do= ONLY if the Request URI is classifieds/ and redirects to the /ads/ URI:  http://www.example.com/classifieds/?do=detailpage&cat=3&id=14 to http://www.example.com/ads/ 

    # Redirects ONLY if the URI is classifieds/ and Strips the do= Query Strings
    # from the destination /ads/ URL|URI
    RewriteCond %{QUERY_STRING} ^do=(.*)$ [NC]
    RewriteRule ^classifieds/$ http://www.example.com/ads/$1? [R=301,L]

    If you are just trying to redirect all old URL’s|URI’s to one place/any new URL|URI on the new site just to get rid of any/all 404 errors from the old site then you can just use this code: RedirectMatch 301 ^/(.*)$ http://www.example.com/

    If you are trying to redirect old URL’s|URI’s to new specific URL’s|URI’s your actual code would probably be something like this and since this code is going to be in the htaccess file on the “old site” then you do not have to factor in that it would impact/affect the URI’s on the new site.

    RewriteCond %{QUERY_STRING} ^p=164$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/new-page-or-post-url/$1? [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=6320$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/another-new-page-or-post-url/$1? [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=2357$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/some-other-new-page-or-post-url/$1? [R=301,L]
    #28567
    DKK
    Participant

    Thank you for your quick & detailed response. The 3rd/last portion you showed looked quite similar to the format I had constructed that I was going to try if you hadn’t replied that was based on examples I found on different forums, but yet you saying that such would be for the “old site” threw me so-to-speak.

    I should have been a bit more specific in my examples and my situation; which was my fault indeed.

    I had managed a website ending in .com that was on shared hosting via GoDaddy.

    Later, the owner purchased their own VPS and a .org version of the Domain (and thus had a .com & .org version of the same Website name –though the actual sites were structured differently). After a period of time, the .com website hosting was purposely let-go (while keeping the domain of course). Thus, the .com needed/needs to be synced to the .org when it comes to all similar/parallel products, pages, categories, links, etc. (as the .com had already made a name for itself on search engines, forums, and review boards).

    When it comes to general redirection, the .com has already been made into a DNS alias & has an overall forwarding-config going to the .org set-up via GoDaddy’s DNS/Domain services (and it has been working perfectly). But for old page names & numbers which are different on the new .org site, 400’s are of course thrown unless proper redirects are put in place.

    Via viewing this forum, I was able to correct URL name mismatches. But it is the "http://website.com/?p=####" to "https://website.org/page/of-something/" sort of mismatch that I’m having issues with. (And yes, the .com was HTTP and the .org is HTTPS; though I have yet to see an issue with that.)

    Thus, the I cannot put the code into the htaccess file on the “old site”, as the old site technically doesn’t exist anymore; well, actually, technically speaking, the .org’s htaccess is now the .com’s htaccess seeming that the one is now the alias of the other…

    I’m not sure if I’ve just clarified that your code should in-fact still work in my situation or not, but I rather be safe than sorry.

    Lastly, just to show in-case it may be of aid, here is the format I was planning on using based on another example:

    # Rewrite rules per example of:
    #RewriteEngine On
    #RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
    #RewriteRule ^(.*)$ http://www.sample-site.com/path/to-proper/page/ [R=302,L]
    
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^p=([167]*)$
    RewriteRule ^/$ https://website.org/new-page/ [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=([3048]*)$
    RewriteRule ^/$ https://website.org/topic/sub-topic/page/ [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=([3068]*)$
    RewriteRule ^/$ https://website.org/category/subcategory/product/index/page/ [R=301,L]
    # END Custom Redirects for Query Strings
    #28570
    AITpro Admin
    Keymaster

    Not sure if you are asking a question or not of if you are saying you already have a solution or you are saying that you are going to try your solution.  In any case, just let me know if your solution does not work and I will try to assist you further.

    #28572
    DKK
    Participant

    lol. My bad. Seems in my attempt to clarify my issue, I sealed it with confusion.  I am asking whether or not your solution of

    RewriteCond %{QUERY_STRING} ^p=164$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/new-page-or-post-url/$1? [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=6320$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/another-new-page-or-post-url/$1? [R=301,L]
    RewriteCond %{QUERY_STRING} ^p=2357$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/some-other-new-page-or-post-url/$1? [R=301,L]

    would still be applicable for my situation even though it would be going in the ‘new’ site’s htaccess rather than the ‘old’?

    (I meant to end my last post with that.)

    #28574
    AITpro Admin
    Keymaster

    Ok so if understand correctly you have DNS pointing all URL’s from the old site to the new site so the URL’s would be this:  new-site.org/?p=164. If that is the case then the code I posted above should work in the new site’s root htaccess file, but there could be a problem with WordPress internal rewriting of p= URL’s (posts and pages).

    If not and the old site URL’s are this: old-site.com/?p=164 then the only way to redirect the old site URL’s to the new site URL’s would be to recreate the old site folder and put a new htaccess file in it with the code I posted above to redirect the old site URL’s that go to the old site and not the new site. The logic is this: the old site URL’s send someone to the old site that existed before and do not send someone to the new site so adding htaccess redirect code on the new site would not do anything since the URL’s do not go/point to this new site.

    #28769
    D Joseph
    Participant

    [Topic has been merged into this relevant Topic]
    Hi, I haven’t worked on websites or touched the BPS  Pro in almost 2 years.

    With the WPTwin, I recently cloned a website that has BPS Pro Version 5.1.9, to a new site. I just cloned the whole thing, uninstalled BPS Pro on the new site through WP admin, and then updated WP to 4.4.2. (Although I still see some BPS folders in Filezilla after I uninstalled the BPS Pro.) I want to do a fresh install on the new site but I need to do this first:

    I need to redirect the 1st domain to the new website. When I tried to do that in CPanel, it wouldn’t let me. I assume it’s because of the BPS Pro security. Can you tell me please, or point me to a link, to the directions on what I need to do for that? Thanks!

    #28772
    AITpro Admin
    Keymaster

    This Redirect htaccess code below in an htaccess file in the old site’s folder would redirect all old site pages and posts to identical matching pages and posts with the same identical structure on the new site/domain.

    RedirectMatch 301 ^/$ http://www.newdomain.com/$1

    For some web hosts you need to include the folder name where the website is physically installed. olddomain.com would be the name of the folder and not the URL|URI name.

    RedirectMatch 301 ^/olddomain.com$ http://www.newdomain.com/$1

    Another example where the old site previously existed in a subfolder and the site was moved to a root folder:
    Old site location: http://www.example.com/old-folder/
    New site location: http://www.example.com/

    RedirectMatch 301 ^/old-folder/(.*)$ http://www.example.com/$1

    See this forum topic for recommendations when migrating/moving/cloning websites:  http://forum.ait-pro.com/forums/topic/migrating-moving-or-cloning-websites/#post-20407

    #30563
    jenni101
    Participant

    Hi there,

    I’ve read and re-read about redirection for an old page to a new page, thought I’d ‘got it’, but my code doesn’t seem to work… Please could you check my code to see if it’s correct for the situation?

    Situation:

    • developing a new site, but with all pages imported from the old site into the new site and re-using as many of these as possible.
    • Old site was a root install
    • New site is in its own folder BUT I’ve changed the NEW site’s url to point to the root in the general settings, as in GWIOD guidelines.
    • So the new site’s url is the same as the old site’s url – and updated all urls with Velvet Blues url updater and all working fine (phew!).

    So I have some old pages in my new site that need to be redirected: to other pages, or to my home page, or to another totally new site I have.

    So for the 3 redirects egs. I have:

    • page http://www.my-site.com/competitions/ —>  to http://www.my-site.com/index.php
    • page http://www.my-site.com/copyright-info/ —>  to http://www.my-site.com/terms-and-conditions
    • page http://www.my-site.com/hzp-group/ —>  to http://www.my-other-site.com/index.php

    and the code I have put in the Bottom custom code box in the .htaccess for the 3 egs. above is this:

    #301 Redirect Old Files JP edit
    Redirectmatch 301 ^/competitions$ http://www.my-site.com/$1
    Redirectmatch 301 ^/copyright-info$ http://www.my-site.com/terms-and-conditions$1
    Redirectmatch 301 ^/hzp-group$ http://www.my-other-site.com/$1

    Any ideas why it’s not working?

    Additional info…

    • Old site is still my current site
    • new site was initially developed in a sub-folder of my old site, with WP url and site url the same ie: www.mysite.com/newsite
    • I’ve since migrated the new site to a new server, keeping it the same as above, but obviously now not with the old site in the root.
    • I’m now working on the new site, on the new server, but the DNS is still pointing to my old site.
    • So I’ve edited my computer’s host file to see my new site on the new server, and it’s this site I’m working on.
    • And now I’ve changed the new site’s (on the new server) site url to the root, so WP url is the same = www.mysite.com/newsite and site url is now = www.mysite.com

    Hope this helps fill in any gaps?
    many thanks, j

    #30569
    AITpro Admin
    Keymaster

    I’m now working on the new site, on the new server, but the DNS is still pointing to my old site.
    So I’ve edited my computer’s host file to see my new site on the new server, and it’s this site I’m working on.

    These 2 statements above conflict with each other. If the site is hosted on a public host server then changing your local hosts file on your computer will only affect your local server or computer. DNS propagation for a public hosted server is handled by the public host. So I assume you are working on a Local copy of your website that is not publicly hosted on a hosting server and is instead installed locally on a Local Development server on your computer.

    Redirectmatch 301 ^/competitions$ http://www.my-site.com/$1 - will redirect to this destination URL: http://www.my-site.com/competitions
    Redirectmatch 301 ^/copyright-info$ http://www.my-site.com/terms-and-conditions$1 - will redirect to this destination URL: http://www.my-site.com/terms-and-conditions/copyright-info
    Redirectmatch 301 ^/hzp-group$ http://www.my-other-site.com/$1 - will redirect to this destination URL: http://www.my-other-site.com/hzp-group
    #30573
    jenni101
    Participant

    @ ait-pro – thanks for your reply.

    So my redirects code is totally incorrect! And when I test it (not logged in) it gives me a 404.

    But i don’t see how that is different to your first example – so why will yours work and mine won’t? As you recommend using the ^ and $ signs at the beginning and end, how can I redirect it to another file?

    Would it work better if I just did it like this? (as I’ve seen it recommended elsewhere):

    #301 Redirect Old Files JP edit
    Redirectmatch 301 /competitions/ http://www.my-site.com/
    Redirectmatch 301 /copyright-info/ http://www.my-site.com/terms-and-conditions/
    Redirectmatch 301 /hzp-group/ http://www.my-other-site.com/

    or like this…

    #301 Redirect Old Files JP edit
    Redirectmatch 301 http://www.my-site.com/competitions/  http://www.my-site.com/
    Redirectmatch 301 http://www.my-site.com/copyright-info/  http://www.my-site.com/terms-and-conditions/
    Redirectmatch 301 http://www.my-site.com/hzp-group/  http://www.my-other-site.com/

    Your thoughts appreciated on this.

    PS: re. my dev site. It is on a public server and the server techs sent me this guide (http://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/) to edit my local pc hosts file to see it. So I can see it on my pc and can check it against my live site on my laptop, where I haven’t edited the hosts file. Seems a brilliant trick to me!

    #30574
    AITpro Admin
    Keymaster

    @jenni101 –

    You should always use the ^ and $ code characters which mean the start and end of a URL path that you want to redirect from (see examples below).  A back reference $1 (see examples below) will look for the $ in the first part of the RedirecMatch URL and is basically something like a placeholder or reference point or an “URL splicer”.  You can of course have many back references ($1, $2, $3, etc) in more complex htaccess code, but for simple RedirecMatch coding you will only have one back reference.  The reason you should always try to use a ^ starting point and a $ ending point is you could have similar URL’s that also would get redirected that you did not want to get redirected.

    #301 Redirect Old Files JP edit
    Redirectmatch 301 ^/competitions/$ http://www.my-site.com/ - will redirect old-site.com/competitions to http://www.my-site.com/
    Redirectmatch 301 ^/copyright-info/$ http://www.my-site.com/terms-and-conditions/ - will redirect old-site.com/copyright-info to http://www.my-site.com/terms-and-conditions 
    Redirectmatch 301 ^/hzp-group/$ http://www.my-other-site.com/ - will redirect old-site.com/hzp-group to http://www.my-other-site.com/
    #30575
    jenni101
    Participant

    OK, so I need to put a ‘/’ after the name of the page I want to redirect – for it to work correctly?

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