Home › Forums › BulletProof Security Pro › htaccess Redirect Code – Where Do I Add Redirect htaccess Code
Tagged: htaccess Redirect Code, RedirecMatch, Redirect
This topic contains 0 replies, has 1 voice, and was last updated by
AITpro Admin 5 months, 3 weeks ago.
-
AuthorPosts
-
November 21, 2012 at 12:22 am #417
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. If you use the Custom Code boxes in BPS Pro then you would use the CUSTOM CODE BOTTOM: text box.
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 $ which mean the start and end of a URL path that you want to redirect and then use $1 which is called a back reference. The back reference $1 will look for the $ in the first part of the RedirecMatch URL and is basically something like a placeholder or reference point or a “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$ /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-blah2By 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.
This code says that if a Request for this URL /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 is spliced together by using $1.
RewriteEngine On RewriteBase /blah/ RewriteCond %{REQUEST_URI} ^/blah/testing-appending-query-string/(.*) [NC] RewriteRule ^(.*)$ $1?keywords=[keyword] [R=302]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.
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]-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 6 months ago by
AITpro Admin.
-
This topic was modified 5 months, 3 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 3 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 5 months, 2 weeks ago by
AITpro Admin.
-
This topic was modified 1 month, 3 weeks ago by
AITpro Admin.
-
This topic was modified 6 months ago by
-
AuthorPosts
You must be logged in to reply to this topic.
