Redirect 301 or RedirectMatch htaccess code

Home Forums BulletProof Security Free Redirect 301 or RedirectMatch htaccess code

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #3219
    Mik
    Member

    I have nearly 1600 individual static html files to redirect that are in the same root folder as the WordPress install. I have tried putting some of them in the BPS Custom Code section, trying bothe the before and after options and neither work. I have also tried inserting them manually into the .htaccess file, again trying both before and after the BPS code and this brings a 500 error when trying to call a static file.
    Each file has to be redirected separately.

    I am wondering if I am going to have to dump BPS although that would really be a shame. Does this happen in the pro version as well?

    #3220
    AITpro Admin
    Keymaster

    If you are seeing a 500 Internal Server Error then you have created/added invalid code.  In other words, you have a typo/mistake somewhere in your code or the code you are adding is not correct/valid code.

    Redirect code is normally stand-alone code and should normally be added at the end of your .htaccess file.  Anywhere outside the WordPress .htaccess rewrite loop is ok – BEGIN WordPress to END WordPress.

    Are you sure you need to create individual redirects and cannot use code like this.

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

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

    NOTE:  This example code MUST come before BEGIN WordPress rewriterules due to the fact that this code is rewriting URL levels.

    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.

    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.

    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]
    #3229
    Mik
    Member

    I cannot use your suggested code as the file and folder names are not exactly the same. A very small example is as follows, it is the standard format for redirection so I cannot see how it is invalid:

    Redirect 301 /renewable_energy/Renewable_Energy_Dictionary.html http://autonopedia.org/renewable-energy/renewable-energy-dictionary/
    Redirect 301 /renewable_energy/Wasted_Energy_and_its_Recovery.html http://autonopedia.org/renewable-energy/wasted-energy-and-its-recovery/
    Redirect 301 /renewable_energy/Biofuels/Index_Bio-Fuels.htm   http://autonopedia.org/renewable-energy/biofuels/
    Redirect 301 /renewable_energy/Biofuels/Background_Info_on_Biodiesel.html http://autonopedia.org/renewable-energy/biofuels/background-info-on-biodiesel/

    As I said, if I paste it into BPS custom code box then nothing at all happens and  if I download the BPS generated .htaccess file, paste the redirects into it and then upload again I get a 500 error. Presumably  the error is because something in the BPS code is clashing as the redirect code is standard …

    #3231
    AITpro Admin
    Keymaster

    The primary reason I prefer RedirectMatch over plain old Redirect is that with RedirectMatch you can also use Regular Expressions / Regex.  With Redirect you cannot use Regex.  They are both identical except for that.

    Source:  http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch

    Your code looks ok so what I assume is happening is that after you paste your code into Custom Code you are not doing the rest of the steps:  Save your Custom Code, go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    Regarding copy and pasting the code into the downloaded .htaccess file on your computer.  Are you using Notepad (not Word or WordPad or some other text editor)?  If your code is actually good then what is happening is that hidden formatting has been added to the .htaccess file.  You can ONLY work with Notepad or even better is Notepad++ for this reason.  The result of that hidden formatting in the .htaccess file will ALWAYS be a 500 Internal Server Error.

    RedirectMatch 301 ^/renewable_energy/Renewable_Energy_Dictionary.html$ http://autonopedia.org/renewable-energy/renewable-energy-dictionary/
    #32132
    Hannah
    Participant

    Three questions and a clarification needed before I go crazy.

    I know the first is a simple question and the answer may already be here, but I get confused with all the various examples of RedirectMatch 301s with regex when redirecting an entire site to another so I need clarification. My client had an rather extensive WordPress ecommerce site at http://domain.com.  It needed to be converted from his older custom theme to a premium theme after the custom coded theme became incompatible with the latest version of the WP core. I cloned the site to create a dev site at http://domain.com/dev/, moved all the content to the original site after the work was done, and deleted the development site so we wouldn’t have to maintain an unused WP install. Though I had set the dev site not to be indexed, Google went ahead and indexed it anyway. Now I have 404s that need to be fixed. So how would I write the RedirectMatch 301 from /dev to http://domain.com in such a way that all the files/pages redirect to their corresponding pages on the now-permanent site?

    Second question. Same client had created a bunch of pages on the site, pasting the titles from Word, so all the permalinks wound up with %E2%80%8E at the end. I did a db search-replace to remove the string from the end of the permalinks and now Google is reporting hundreds of 404s. How can I bulk redirect all these permalinks to their counterparts without the extra characters appended to the urls?

    The same client’s htaccess has these two rules at the beginning and a substantial number of 301s at the bottom:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^rakufiredceramics.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.rakufiredceramics.com$
    RewriteRule ^/?$ "http\:\/\/raku\-ceramic\-pottery\.com" [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^doderostudioceramics\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.doderostudioceramics\.com$
    RewriteRule ^/?$ "http\:\/\/raku\-ceramic\-pottery\.com\/" [R=301,L]

    Currently SSL is only applied to the shop checkout pages. If we were to make the decision to apply SSL to the entire site for SEO purposes, how would I do the third rewrite?

    And one last thing. In many other references discussing how to work with redirects in htaccess it is said that the redirects should be placed above the WordPress rewrite rules. Yet I’ve seen you state more than once that redirects should all be at the bottom of the htaccess file.

    Thanks so much, knowing how to do this will save me so much time on this project and future ones too. I’ve learned a lot but these are nagging questions that seem to crop up from time to time, and I’ve never been able to sort out all the online instructions I find to answer them. I so appreciate your knowledge of how to most effectively use .htaccess and look forward to your guidance.

    #32133
    AITpro Admin
    Keymaster

    @ Hannah – To redirect the old site to the new site use this code in the old site’s root htaccess file, which would be the htaccess file where the old site was installed.  All you need is this one line of htaccess code.

    RedirectMatch 301 ^/(.*)$ http://www.example.com/dev/$1

    Second question: I don’t have enough information. I would need a bad URL and the good URL that you want it to redirect too. Also if all the bad URL’s are not similar in structure then that could be very difficult to solve with htaccess code in a bulk way. Example similar URI structure: example.com/path-to-mess/fubar%E2%80%8E/

    You can add that existing redirect code in the bottom Custom Code text box with any other htaccess redirect code.

    You want to make the entire site SSL. Google will be penalizing sites starting very soon if the entire site is not SSL. Site’s that are SSL will load faster, rank higher in Google and have “Secure” in the Browser address bar like this forum site has. See this forum topic for htaccess SSL code and the steps to add it to Custom Code: https://forum.ait-pro.com/forums/topic/wordpress-ssl-htaccess-code-rewrite-ssl-rewritecond-server_port/#post-7233

    Most htacces RedirectMatch code can go anywhere in an htaccess file. So the Custom Code bottom text box is the best place for RedirectMatch code just for visual clarity/neatness/order.

    #32135
    Hannah
    Participant

    Thank you.

    1 – I found an existing htaccess file in /dev/ and replaced the code with the line you provided. Gave it a couple minutes and tested by clicking a few of the 404 links in Search Console (after refreshing). I got an error “The page isn’t redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete…”  The url has a string of many /dev/dev/dev/ before it completes the url and ends. Did I do something wrong? Is there anything other than htaccess that I should check?

    2 – Looks like the lack of similarity in many of those links is going to require doing them separately

    3 – I’m going to strongly suggest SSL on the entire site to him. He’s very interested in maintaining good SEO practices so I’m sure he will approve this. Thanks for the link to instructions.

    #32140
    AITpro Admin
    Keymaster

    @ Hannah –
    1.  The redirect needs to go in the htaccess file where the site was previously installed and not the /dev/ htaccess file because that is where the site has been moved to.  When someone clicks an old URL they are going to be sent to the old URL and not the /dev/ URL. If an old htaccess file no longer exists for the old site in the old site folder then create a new htaccess file and put that 1 line of code in it to redirect to the new /dev/ site.

    #32144
    Hannah
    Participant

    Hm. Either I haven’t understood you correctly or I may have confused you. To clarify, the contents of the /dev/ site have been moved to the root, so the /dev/ site is actually the “old” site now. We want traffic from /dev/ to go to the root to resolve the 404s Google is reporting for /dev/.

    #32145
    AITpro Admin
    Keymaster

    @ Hannah – Yep, you definitely confused me. 😉  Even re-reading your original post it still sounds like you moved the site from / to /dev/. 😉  Ok so the code you need to use is this code below which goes in your new root htaccess file for the new root site installed in the root folder /.

    1. Copy this redirect code to this BPS Root Custom Code text box:  CUSTOM CODE TOP PHP/PHP.INI HANDLER/CACHE CODE
    2. Click the Save Root Custom Code button.
    3. Activate Root Folder BulletProof Mode.
    Notes: Edit the code (dev, the site domain URL) to match your actual site. If you decide to make the entire site https then change the URL Scheme to https.

    RedirectMatch 301 ^/dev/(.*)$ http://www.example.com/$1

    For future reference it would simpler and clearer if you use this format to explain things. Also you would not need to type additional stuff to explain the scenario since everything you need to say can be done with just these 2 lines of text below.
    Old site|URL: example.com/dev/
    New site|URL: example.com/

    #32153
    Hannah
    Participant

    I’m so sorry about that! Thanks for your patience with my verbose descriptions. AND most of all for the working htaccess code! That took care of the 157 404s Google has found in one moment. You are so awesome.

    #32154
    AITpro Admin
    Keymaster

    @ Hannah – Great!  No need to apologize for anything.  I just thought suggesting a “tip” on the best format to post in topics would speed up the process of getting a working solution. 😉

    Oh and if you want to get rid of all the 404 errors for the URL Encoded Microsoft Word hidden formatting characters:  â€Ž in URLs in a bulk way you can use this code below.  Unfortunately, that means that any URLs indexed by google and other search engines with the hidden formatting characters in the URLs will not be redirected to the new fixed URLs, unless you create individual redirect rules.  What this will do is send all old indexed URLs to a new page that you create on the site or an existing page just to handle/fix the 404 errors.

    RedirectMatch 301 ^/.*‎.*$ http://www.example.com/somewhere/

    Also if you have a Google Webmasters Tools account then you can use the Google Index > Remove URLs tool to de-index/remove any bad URLs with the Microsoft Word hidden formatting characters in them.

    #32159
    Hannah
    Participant

    Thanks for the tip, Keymaster. I’ll try to remember that in the future.

    That’s good to know. I already had most of those urls “fixed” in htaccess but it was not working. I transferred them to the Redirection plugin I installed a few months ago and those worked. I don’t think we’ll be seeing any more of those since they cropped up a while back, so I think it was worth the time to do them individually.. Now I’m hoping I can pull off the https without too much trouble.

    On the redirects already present at the top of his htaccess file, would I just add the “s” to the “http” in both of these, as well as the redirect you gave me for /dev/ to the root? Will modifying these three take care of the whole thing, or will I still need another one from the page you referred me to earlier? Just don’t want to start any redirect loops…

    RewriteCond %{HTTP_HOST} ^rakufiredceramics.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.rakufiredceramics.com$
    RewriteRule ^/?$ "https\:\/\/raku\-ceramic\-pottery\.com" [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^doderostudioceramics\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.doderostudioceramics\.com$
    RewriteRule ^/?$ "https\:\/\/raku\-ceramic\-pottery\.com\/" [R=301,L]
    RedirectMatch 301 ^/dev/(.*)$ https://www.example.com/$1
    #32160
    AITpro Admin
    Keymaster

    @ Hannah – Those redirects go to other or different websites or maybe the same website.  I cannot really tell what is happening and I would need to know how those websites relate to the website you are working on and why that redirect code is being used on the current website.  Also you cannot just change http to https in that type of redirect code since the redirects would not work correctly.  You would need to use this SSL/HTTPS Rewrite/Redirect code here:  https://forum.ait-pro.com/forums/topic/wordpress-ssl-htaccess-code-rewrite-ssl-rewritecond-server_port/#post-7233

    The link above to the forum topic about correct SSL/HTTPS htaccess code is just for a general example. In your case you are going to need a custom solution. What that custom solution is I cannot tell you until I know all of this information.
    Current website domain name: X
    Old or other website domain names: reason for using the htaccess code in a different website’s htaccess file. My guess would be the owner of the site changed the domain name at some point and was forwarding URL’s to the new domain name. Forwarding would have to have been used in order for that htaccess code to do anything. Or the other possibility is the owner of the old site had nested domains, aliased domains, etc.

    It may be that old htaccess code is no longer needed. So post all of the necessary information when you get that information.

    #32170
    Hannah
    Participant

    Just heard back from him. “When I changed over my Zen Cart to use WordPress for the cart we changed the URL  supposedly for better SEO, as I remember not wanting to lose traffic we redirected from doderostudioceramics.com.” Apparently his records and memory are a little vague, but as far as I can tell, both of these redirects are from previously used domains that he owns. When I first started working with him, the domain on the main html site was doderostudioceramics.com and the WordPress/WooCommerce site was doderostudioceramics.com/shop. We merged them both into a new WordPress site, but that was after he’d changed the url to raku-ceramic-pottery.com to take advantage of a keyword-based domain name, and that’s the domain we merged everything into.

    The current website domain is http://raku-ceramic-pottery.com

    We hope that’s helpful.

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